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 {"cylindrical", "cylindrical", 0, AV_OPT_TYPE_CONST, {.i64=CYLINDRICAL}, 0, 0, FLAGS, "in" },
78 {"tetrahedron", "tetrahedron", 0, AV_OPT_TYPE_CONST, {.i64=TETRAHEDRON}, 0, 0, FLAGS, "in" },
79 {"barrelsplit", "barrel split facebook's 360 format", 0, AV_OPT_TYPE_CONST, {.i64=BARREL_SPLIT}, 0, 0, FLAGS, "in" },
80 { "output", "set output projection", OFFSET(out), AV_OPT_TYPE_INT, {.i64=CUBEMAP_3_2}, 0, NB_PROJECTIONS-1, FLAGS, "out" },
81 { "e", "equirectangular", 0, AV_OPT_TYPE_CONST, {.i64=EQUIRECTANGULAR}, 0, 0, FLAGS, "out" },
82 { "equirect", "equirectangular", 0, AV_OPT_TYPE_CONST, {.i64=EQUIRECTANGULAR}, 0, 0, FLAGS, "out" },
83 { "c3x2", "cubemap 3x2", 0, AV_OPT_TYPE_CONST, {.i64=CUBEMAP_3_2}, 0, 0, FLAGS, "out" },
84 { "c6x1", "cubemap 6x1", 0, AV_OPT_TYPE_CONST, {.i64=CUBEMAP_6_1}, 0, 0, FLAGS, "out" },
85 { "eac", "equi-angular cubemap", 0, AV_OPT_TYPE_CONST, {.i64=EQUIANGULAR}, 0, 0, FLAGS, "out" },
86 { "dfisheye", "dual fisheye", 0, AV_OPT_TYPE_CONST, {.i64=DUAL_FISHEYE}, 0, 0, FLAGS, "out" },
87 { "flat", "regular video", 0, AV_OPT_TYPE_CONST, {.i64=FLAT}, 0, 0, FLAGS, "out" },
88 {"rectilinear", "regular video", 0, AV_OPT_TYPE_CONST, {.i64=FLAT}, 0, 0, FLAGS, "out" },
89 { "gnomonic", "regular video", 0, AV_OPT_TYPE_CONST, {.i64=FLAT}, 0, 0, FLAGS, "out" },
90 { "barrel", "barrel facebook's 360 format", 0, AV_OPT_TYPE_CONST, {.i64=BARREL}, 0, 0, FLAGS, "out" },
91 { "fb", "barrel facebook's 360 format", 0, AV_OPT_TYPE_CONST, {.i64=BARREL}, 0, 0, FLAGS, "out" },
92 { "c1x6", "cubemap 1x6", 0, AV_OPT_TYPE_CONST, {.i64=CUBEMAP_1_6}, 0, 0, FLAGS, "out" },
93 { "sg", "stereographic", 0, AV_OPT_TYPE_CONST, {.i64=STEREOGRAPHIC}, 0, 0, FLAGS, "out" },
94 { "mercator", "mercator", 0, AV_OPT_TYPE_CONST, {.i64=MERCATOR}, 0, 0, FLAGS, "out" },
95 { "ball", "ball", 0, AV_OPT_TYPE_CONST, {.i64=BALL}, 0, 0, FLAGS, "out" },
96 { "hammer", "hammer", 0, AV_OPT_TYPE_CONST, {.i64=HAMMER}, 0, 0, FLAGS, "out" },
97 {"sinusoidal", "sinusoidal", 0, AV_OPT_TYPE_CONST, {.i64=SINUSOIDAL}, 0, 0, FLAGS, "out" },
98 { "fisheye", "fisheye", 0, AV_OPT_TYPE_CONST, {.i64=FISHEYE}, 0, 0, FLAGS, "out" },
99 { "pannini", "pannini", 0, AV_OPT_TYPE_CONST, {.i64=PANNINI}, 0, 0, FLAGS, "out" },
100 {"cylindrical", "cylindrical", 0, AV_OPT_TYPE_CONST, {.i64=CYLINDRICAL}, 0, 0, FLAGS, "out" },
101 {"perspective", "perspective", 0, AV_OPT_TYPE_CONST, {.i64=PERSPECTIVE}, 0, 0, FLAGS, "out" },
102 {"tetrahedron", "tetrahedron", 0, AV_OPT_TYPE_CONST, {.i64=TETRAHEDRON}, 0, 0, FLAGS, "out" },
103 {"barrelsplit", "barrel split facebook's 360 format", 0, AV_OPT_TYPE_CONST, {.i64=BARREL_SPLIT}, 0, 0, FLAGS, "out" },
104 { "interp", "set interpolation method", OFFSET(interp), AV_OPT_TYPE_INT, {.i64=BILINEAR}, 0, NB_INTERP_METHODS-1, FLAGS, "interp" },
105 { "near", "nearest neighbour", 0, AV_OPT_TYPE_CONST, {.i64=NEAREST}, 0, 0, FLAGS, "interp" },
106 { "nearest", "nearest neighbour", 0, AV_OPT_TYPE_CONST, {.i64=NEAREST}, 0, 0, FLAGS, "interp" },
107 { "line", "bilinear interpolation", 0, AV_OPT_TYPE_CONST, {.i64=BILINEAR}, 0, 0, FLAGS, "interp" },
108 { "linear", "bilinear interpolation", 0, AV_OPT_TYPE_CONST, {.i64=BILINEAR}, 0, 0, FLAGS, "interp" },
109 { "cube", "bicubic interpolation", 0, AV_OPT_TYPE_CONST, {.i64=BICUBIC}, 0, 0, FLAGS, "interp" },
110 { "cubic", "bicubic interpolation", 0, AV_OPT_TYPE_CONST, {.i64=BICUBIC}, 0, 0, FLAGS, "interp" },
111 { "lanc", "lanczos interpolation", 0, AV_OPT_TYPE_CONST, {.i64=LANCZOS}, 0, 0, FLAGS, "interp" },
112 { "lanczos", "lanczos interpolation", 0, AV_OPT_TYPE_CONST, {.i64=LANCZOS}, 0, 0, FLAGS, "interp" },
113 { "sp16", "spline16 interpolation", 0, AV_OPT_TYPE_CONST, {.i64=SPLINE16}, 0, 0, FLAGS, "interp" },
114 { "spline16", "spline16 interpolation", 0, AV_OPT_TYPE_CONST, {.i64=SPLINE16}, 0, 0, FLAGS, "interp" },
115 { "gauss", "gaussian interpolation", 0, AV_OPT_TYPE_CONST, {.i64=GAUSSIAN}, 0, 0, FLAGS, "interp" },
116 { "gaussian", "gaussian interpolation", 0, AV_OPT_TYPE_CONST, {.i64=GAUSSIAN}, 0, 0, FLAGS, "interp" },
117 { "w", "output width", OFFSET(width), AV_OPT_TYPE_INT, {.i64=0}, 0, INT16_MAX, FLAGS, "w"},
118 { "h", "output height", OFFSET(height), AV_OPT_TYPE_INT, {.i64=0}, 0, INT16_MAX, FLAGS, "h"},
119 { "in_stereo", "input stereo format", OFFSET(in_stereo), AV_OPT_TYPE_INT, {.i64=STEREO_2D}, 0, NB_STEREO_FMTS-1, FLAGS, "stereo" },
120 {"out_stereo", "output stereo format", OFFSET(out_stereo), AV_OPT_TYPE_INT, {.i64=STEREO_2D}, 0, NB_STEREO_FMTS-1, FLAGS, "stereo" },
121 { "2d", "2d mono", 0, AV_OPT_TYPE_CONST, {.i64=STEREO_2D}, 0, 0, FLAGS, "stereo" },
122 { "sbs", "side by side", 0, AV_OPT_TYPE_CONST, {.i64=STEREO_SBS}, 0, 0, FLAGS, "stereo" },
123 { "tb", "top bottom", 0, AV_OPT_TYPE_CONST, {.i64=STEREO_TB}, 0, 0, FLAGS, "stereo" },
124 { "in_forder", "input cubemap face order", OFFSET(in_forder), AV_OPT_TYPE_STRING, {.str="rludfb"}, 0, NB_DIRECTIONS-1, FLAGS, "in_forder"},
125 {"out_forder", "output cubemap face order", OFFSET(out_forder), AV_OPT_TYPE_STRING, {.str="rludfb"}, 0, NB_DIRECTIONS-1, FLAGS, "out_forder"},
126 { "in_frot", "input cubemap face rotation", OFFSET(in_frot), AV_OPT_TYPE_STRING, {.str="000000"}, 0, NB_DIRECTIONS-1, FLAGS, "in_frot"},
127 { "out_frot", "output cubemap face rotation",OFFSET(out_frot), AV_OPT_TYPE_STRING, {.str="000000"}, 0, NB_DIRECTIONS-1, FLAGS, "out_frot"},
128 { "in_pad", "percent input cubemap pads", OFFSET(in_pad), AV_OPT_TYPE_FLOAT, {.dbl=0.f}, 0.f, 1.f,TFLAGS, "in_pad"},
129 { "out_pad", "percent output cubemap pads", OFFSET(out_pad), AV_OPT_TYPE_FLOAT, {.dbl=0.f}, 0.f, 1.f,TFLAGS, "out_pad"},
130 { "fin_pad", "fixed input cubemap pads", OFFSET(fin_pad), AV_OPT_TYPE_INT, {.i64=0}, 0, 100,TFLAGS, "fin_pad"},
131 { "fout_pad", "fixed output cubemap pads", OFFSET(fout_pad), AV_OPT_TYPE_INT, {.i64=0}, 0, 100,TFLAGS, "fout_pad"},
132 { "yaw", "yaw rotation", OFFSET(yaw), AV_OPT_TYPE_FLOAT, {.dbl=0.f}, -180.f, 180.f,TFLAGS, "yaw"},
133 { "pitch", "pitch rotation", OFFSET(pitch), AV_OPT_TYPE_FLOAT, {.dbl=0.f}, -180.f, 180.f,TFLAGS, "pitch"},
134 { "roll", "roll rotation", OFFSET(roll), AV_OPT_TYPE_FLOAT, {.dbl=0.f}, -180.f, 180.f,TFLAGS, "roll"},
135 { "rorder", "rotation order", OFFSET(rorder), AV_OPT_TYPE_STRING, {.str="ypr"}, 0, 0,TFLAGS, "rorder"},
136 { "h_fov", "horizontal field of view", OFFSET(h_fov), AV_OPT_TYPE_FLOAT, {.dbl=90.f}, 0.00001f, 360.f,TFLAGS, "h_fov"},
137 { "v_fov", "vertical field of view", OFFSET(v_fov), AV_OPT_TYPE_FLOAT, {.dbl=45.f}, 0.00001f, 360.f,TFLAGS, "v_fov"},
138 { "d_fov", "diagonal field of view", OFFSET(d_fov), AV_OPT_TYPE_FLOAT, {.dbl=0.f}, 0.f, 360.f,TFLAGS, "d_fov"},
139 { "h_flip", "flip out video horizontally", OFFSET(h_flip), AV_OPT_TYPE_BOOL, {.i64=0}, 0, 1,TFLAGS, "h_flip"},
140 { "v_flip", "flip out video vertically", OFFSET(v_flip), AV_OPT_TYPE_BOOL, {.i64=0}, 0, 1,TFLAGS, "v_flip"},
141 { "d_flip", "flip out video indepth", OFFSET(d_flip), AV_OPT_TYPE_BOOL, {.i64=0}, 0, 1,TFLAGS, "d_flip"},
142 { "ih_flip", "flip in video horizontally", OFFSET(ih_flip), AV_OPT_TYPE_BOOL, {.i64=0}, 0, 1,TFLAGS, "ih_flip"},
143 { "iv_flip", "flip in video vertically", OFFSET(iv_flip), AV_OPT_TYPE_BOOL, {.i64=0}, 0, 1,TFLAGS, "iv_flip"},
144 { "in_trans", "transpose video input", OFFSET(in_transpose), AV_OPT_TYPE_BOOL, {.i64=0}, 0, 1, FLAGS, "in_transpose"},
145 { "out_trans", "transpose video output", OFFSET(out_transpose), AV_OPT_TYPE_BOOL, {.i64=0}, 0, 1, FLAGS, "out_transpose"},
146 { "ih_fov", "input horizontal field of view",OFFSET(ih_fov), AV_OPT_TYPE_FLOAT, {.dbl=90.f}, 0.00001f, 360.f,TFLAGS, "ih_fov"},
147 { "iv_fov", "input vertical field of view", OFFSET(iv_fov), AV_OPT_TYPE_FLOAT, {.dbl=45.f}, 0.00001f, 360.f,TFLAGS, "iv_fov"},
148 { "id_fov", "input diagonal field of view", OFFSET(id_fov), AV_OPT_TYPE_FLOAT, {.dbl=0.f}, 0.f, 360.f,TFLAGS, "id_fov"},
149 {"alpha_mask", "build mask in alpha plane", OFFSET(alpha), AV_OPT_TYPE_BOOL, {.i64=0}, 0, 1, FLAGS, "alpha"},
153 AVFILTER_DEFINE_CLASS(v360);
155 static int query_formats(AVFilterContext *ctx)
157 V360Context *s = ctx->priv;
158 static const enum AVPixelFormat pix_fmts[] = {
160 AV_PIX_FMT_YUVA444P, AV_PIX_FMT_YUVA444P9,
161 AV_PIX_FMT_YUVA444P10, AV_PIX_FMT_YUVA444P12,
162 AV_PIX_FMT_YUVA444P16,
165 AV_PIX_FMT_YUVA422P, AV_PIX_FMT_YUVA422P9,
166 AV_PIX_FMT_YUVA422P10, AV_PIX_FMT_YUVA422P12,
167 AV_PIX_FMT_YUVA422P16,
170 AV_PIX_FMT_YUVA420P, AV_PIX_FMT_YUVA420P9,
171 AV_PIX_FMT_YUVA420P10, AV_PIX_FMT_YUVA420P16,
174 AV_PIX_FMT_YUVJ444P, AV_PIX_FMT_YUVJ440P,
175 AV_PIX_FMT_YUVJ422P, AV_PIX_FMT_YUVJ420P,
179 AV_PIX_FMT_YUV444P, AV_PIX_FMT_YUV444P9,
180 AV_PIX_FMT_YUV444P10, AV_PIX_FMT_YUV444P12,
181 AV_PIX_FMT_YUV444P14, AV_PIX_FMT_YUV444P16,
184 AV_PIX_FMT_YUV440P, AV_PIX_FMT_YUV440P10,
185 AV_PIX_FMT_YUV440P12,
188 AV_PIX_FMT_YUV422P, AV_PIX_FMT_YUV422P9,
189 AV_PIX_FMT_YUV422P10, AV_PIX_FMT_YUV422P12,
190 AV_PIX_FMT_YUV422P14, AV_PIX_FMT_YUV422P16,
193 AV_PIX_FMT_YUV420P, AV_PIX_FMT_YUV420P9,
194 AV_PIX_FMT_YUV420P10, AV_PIX_FMT_YUV420P12,
195 AV_PIX_FMT_YUV420P14, AV_PIX_FMT_YUV420P16,
204 AV_PIX_FMT_GBRP, AV_PIX_FMT_GBRP9,
205 AV_PIX_FMT_GBRP10, AV_PIX_FMT_GBRP12,
206 AV_PIX_FMT_GBRP14, AV_PIX_FMT_GBRP16,
209 AV_PIX_FMT_GBRAP, AV_PIX_FMT_GBRAP10,
210 AV_PIX_FMT_GBRAP12, AV_PIX_FMT_GBRAP16,
213 AV_PIX_FMT_GRAY8, AV_PIX_FMT_GRAY9,
214 AV_PIX_FMT_GRAY10, AV_PIX_FMT_GRAY12,
215 AV_PIX_FMT_GRAY14, AV_PIX_FMT_GRAY16,
219 static const enum AVPixelFormat alpha_pix_fmts[] = {
220 AV_PIX_FMT_YUVA444P, AV_PIX_FMT_YUVA444P9,
221 AV_PIX_FMT_YUVA444P10, AV_PIX_FMT_YUVA444P12,
222 AV_PIX_FMT_YUVA444P16,
223 AV_PIX_FMT_YUVA422P, AV_PIX_FMT_YUVA422P9,
224 AV_PIX_FMT_YUVA422P10, AV_PIX_FMT_YUVA422P12,
225 AV_PIX_FMT_YUVA422P16,
226 AV_PIX_FMT_YUVA420P, AV_PIX_FMT_YUVA420P9,
227 AV_PIX_FMT_YUVA420P10, AV_PIX_FMT_YUVA420P16,
228 AV_PIX_FMT_GBRAP, AV_PIX_FMT_GBRAP10,
229 AV_PIX_FMT_GBRAP12, AV_PIX_FMT_GBRAP16,
233 AVFilterFormats *fmts_list = ff_make_format_list(s->alpha ? alpha_pix_fmts : pix_fmts);
235 return AVERROR(ENOMEM);
236 return ff_set_common_formats(ctx, fmts_list);
239 #define DEFINE_REMAP1_LINE(bits, div) \
240 static void remap1_##bits##bit_line_c(uint8_t *dst, int width, const uint8_t *const src, \
241 ptrdiff_t in_linesize, \
242 const int16_t *const u, const int16_t *const v, \
243 const int16_t *const ker) \
245 const uint##bits##_t *const s = (const uint##bits##_t *const)src; \
246 uint##bits##_t *d = (uint##bits##_t *)dst; \
248 in_linesize /= div; \
250 for (int x = 0; x < width; x++) \
251 d[x] = s[v[x] * in_linesize + u[x]]; \
254 DEFINE_REMAP1_LINE( 8, 1)
255 DEFINE_REMAP1_LINE(16, 2)
258 * Generate remapping function with a given window size and pixel depth.
260 * @param ws size of interpolation window
261 * @param bits number of bits per pixel
263 #define DEFINE_REMAP(ws, bits) \
264 static int remap##ws##_##bits##bit_slice(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs) \
266 ThreadData *td = arg; \
267 const V360Context *s = ctx->priv; \
268 const AVFrame *in = td->in; \
269 AVFrame *out = td->out; \
271 for (int stereo = 0; stereo < 1 + s->out_stereo > STEREO_2D; stereo++) { \
272 for (int plane = 0; plane < s->nb_planes; plane++) { \
273 const unsigned map = s->map[plane]; \
274 const int in_linesize = in->linesize[plane]; \
275 const int out_linesize = out->linesize[plane]; \
276 const int uv_linesize = s->uv_linesize[plane]; \
277 const int in_offset_w = stereo ? s->in_offset_w[plane] : 0; \
278 const int in_offset_h = stereo ? s->in_offset_h[plane] : 0; \
279 const int out_offset_w = stereo ? s->out_offset_w[plane] : 0; \
280 const int out_offset_h = stereo ? s->out_offset_h[plane] : 0; \
281 const uint8_t *const src = in->data[plane] + \
282 in_offset_h * in_linesize + in_offset_w * (bits >> 3); \
283 uint8_t *dst = out->data[plane] + out_offset_h * out_linesize + out_offset_w * (bits >> 3); \
284 const uint8_t *mask = plane == 3 ? s->mask : NULL; \
285 const int width = s->pr_width[plane]; \
286 const int height = s->pr_height[plane]; \
288 const int slice_start = (height * jobnr ) / nb_jobs; \
289 const int slice_end = (height * (jobnr + 1)) / nb_jobs; \
291 for (int y = slice_start; y < slice_end && !mask; y++) { \
292 const int16_t *const u = s->u[map] + y * uv_linesize * ws * ws; \
293 const int16_t *const v = s->v[map] + y * uv_linesize * ws * ws; \
294 const int16_t *const ker = s->ker[map] + y * uv_linesize * ws * ws; \
296 s->remap_line(dst + y * out_linesize, width, src, in_linesize, u, v, ker); \
299 for (int y = slice_start; y < slice_end && mask; y++) { \
300 memcpy(dst + y * out_linesize, mask + y * width * (bits >> 3), width * (bits >> 3)); \
315 #define DEFINE_REMAP_LINE(ws, bits, div) \
316 static void remap##ws##_##bits##bit_line_c(uint8_t *dst, int width, const uint8_t *const src, \
317 ptrdiff_t in_linesize, \
318 const int16_t *const u, const int16_t *const v, \
319 const int16_t *const ker) \
321 const uint##bits##_t *const s = (const uint##bits##_t *const)src; \
322 uint##bits##_t *d = (uint##bits##_t *)dst; \
324 in_linesize /= div; \
326 for (int x = 0; x < width; x++) { \
327 const int16_t *const uu = u + x * ws * ws; \
328 const int16_t *const vv = v + x * ws * ws; \
329 const int16_t *const kker = ker + x * ws * ws; \
332 for (int i = 0; i < ws; i++) { \
333 for (int j = 0; j < ws; j++) { \
334 tmp += kker[i * ws + j] * s[vv[i * ws + j] * in_linesize + uu[i * ws + j]]; \
338 d[x] = av_clip_uint##bits(tmp >> 14); \
342 DEFINE_REMAP_LINE(2, 8, 1)
343 DEFINE_REMAP_LINE(4, 8, 1)
344 DEFINE_REMAP_LINE(2, 16, 2)
345 DEFINE_REMAP_LINE(4, 16, 2)
347 void ff_v360_init(V360Context *s, int depth)
351 s->remap_line = depth <= 8 ? remap1_8bit_line_c : remap1_16bit_line_c;
354 s->remap_line = depth <= 8 ? remap2_8bit_line_c : remap2_16bit_line_c;
360 s->remap_line = depth <= 8 ? remap4_8bit_line_c : remap4_16bit_line_c;
365 ff_v360_init_x86(s, depth);
369 * Save nearest pixel coordinates for remapping.
371 * @param du horizontal relative coordinate
372 * @param dv vertical relative coordinate
373 * @param rmap calculated 4x4 window
374 * @param u u remap data
375 * @param v v remap data
376 * @param ker ker remap data
378 static void nearest_kernel(float du, float dv, const XYRemap *rmap,
379 int16_t *u, int16_t *v, int16_t *ker)
381 const int i = lrintf(dv) + 1;
382 const int j = lrintf(du) + 1;
384 u[0] = rmap->u[i][j];
385 v[0] = rmap->v[i][j];
389 * Calculate kernel for bilinear interpolation.
391 * @param du horizontal relative coordinate
392 * @param dv vertical relative coordinate
393 * @param rmap calculated 4x4 window
394 * @param u u remap data
395 * @param v v remap data
396 * @param ker ker remap data
398 static void bilinear_kernel(float du, float dv, const XYRemap *rmap,
399 int16_t *u, int16_t *v, int16_t *ker)
401 for (int i = 0; i < 2; i++) {
402 for (int j = 0; j < 2; j++) {
403 u[i * 2 + j] = rmap->u[i + 1][j + 1];
404 v[i * 2 + j] = rmap->v[i + 1][j + 1];
408 ker[0] = lrintf((1.f - du) * (1.f - dv) * 16385.f);
409 ker[1] = lrintf( du * (1.f - dv) * 16385.f);
410 ker[2] = lrintf((1.f - du) * dv * 16385.f);
411 ker[3] = lrintf( du * dv * 16385.f);
415 * Calculate 1-dimensional cubic coefficients.
417 * @param t relative coordinate
418 * @param coeffs coefficients
420 static inline void calculate_bicubic_coeffs(float t, float *coeffs)
422 const float tt = t * t;
423 const float ttt = t * t * t;
425 coeffs[0] = - t / 3.f + tt / 2.f - ttt / 6.f;
426 coeffs[1] = 1.f - t / 2.f - tt + ttt / 2.f;
427 coeffs[2] = t + tt / 2.f - ttt / 2.f;
428 coeffs[3] = - t / 6.f + ttt / 6.f;
432 * Calculate kernel for bicubic interpolation.
434 * @param du horizontal relative coordinate
435 * @param dv vertical relative coordinate
436 * @param rmap calculated 4x4 window
437 * @param u u remap data
438 * @param v v remap data
439 * @param ker ker remap data
441 static void bicubic_kernel(float du, float dv, const XYRemap *rmap,
442 int16_t *u, int16_t *v, int16_t *ker)
447 calculate_bicubic_coeffs(du, du_coeffs);
448 calculate_bicubic_coeffs(dv, dv_coeffs);
450 for (int i = 0; i < 4; i++) {
451 for (int j = 0; j < 4; j++) {
452 u[i * 4 + j] = rmap->u[i][j];
453 v[i * 4 + j] = rmap->v[i][j];
454 ker[i * 4 + j] = lrintf(du_coeffs[j] * dv_coeffs[i] * 16385.f);
460 * Calculate 1-dimensional lanczos coefficients.
462 * @param t relative coordinate
463 * @param coeffs coefficients
465 static inline void calculate_lanczos_coeffs(float t, float *coeffs)
469 for (int i = 0; i < 4; i++) {
470 const float x = M_PI * (t - i + 1);
474 coeffs[i] = sinf(x) * sinf(x / 2.f) / (x * x / 2.f);
479 for (int i = 0; i < 4; i++) {
485 * Calculate kernel for lanczos interpolation.
487 * @param du horizontal relative coordinate
488 * @param dv vertical relative coordinate
489 * @param rmap calculated 4x4 window
490 * @param u u remap data
491 * @param v v remap data
492 * @param ker ker remap data
494 static void lanczos_kernel(float du, float dv, const XYRemap *rmap,
495 int16_t *u, int16_t *v, int16_t *ker)
500 calculate_lanczos_coeffs(du, du_coeffs);
501 calculate_lanczos_coeffs(dv, dv_coeffs);
503 for (int i = 0; i < 4; i++) {
504 for (int j = 0; j < 4; j++) {
505 u[i * 4 + j] = rmap->u[i][j];
506 v[i * 4 + j] = rmap->v[i][j];
507 ker[i * 4 + j] = lrintf(du_coeffs[j] * dv_coeffs[i] * 16385.f);
513 * Calculate 1-dimensional spline16 coefficients.
515 * @param t relative coordinate
516 * @param coeffs coefficients
518 static void calculate_spline16_coeffs(float t, float *coeffs)
520 coeffs[0] = ((-1.f / 3.f * t + 0.8f) * t - 7.f / 15.f) * t;
521 coeffs[1] = ((t - 9.f / 5.f) * t - 0.2f) * t + 1.f;
522 coeffs[2] = ((6.f / 5.f - t) * t + 0.8f) * t;
523 coeffs[3] = ((1.f / 3.f * t - 0.2f) * t - 2.f / 15.f) * t;
527 * Calculate kernel for spline16 interpolation.
529 * @param du horizontal relative coordinate
530 * @param dv vertical relative coordinate
531 * @param rmap calculated 4x4 window
532 * @param u u remap data
533 * @param v v remap data
534 * @param ker ker remap data
536 static void spline16_kernel(float du, float dv, const XYRemap *rmap,
537 int16_t *u, int16_t *v, int16_t *ker)
542 calculate_spline16_coeffs(du, du_coeffs);
543 calculate_spline16_coeffs(dv, dv_coeffs);
545 for (int i = 0; i < 4; i++) {
546 for (int j = 0; j < 4; j++) {
547 u[i * 4 + j] = rmap->u[i][j];
548 v[i * 4 + j] = rmap->v[i][j];
549 ker[i * 4 + j] = lrintf(du_coeffs[j] * dv_coeffs[i] * 16385.f);
555 * Calculate 1-dimensional gaussian coefficients.
557 * @param t relative coordinate
558 * @param coeffs coefficients
560 static void calculate_gaussian_coeffs(float t, float *coeffs)
564 for (int i = 0; i < 4; i++) {
565 const float x = t - (i - 1);
569 coeffs[i] = expf(-2.f * x * x) * expf(-x * x / 2.f);
574 for (int i = 0; i < 4; i++) {
580 * Calculate kernel for gaussian interpolation.
582 * @param du horizontal relative coordinate
583 * @param dv vertical relative coordinate
584 * @param rmap calculated 4x4 window
585 * @param u u remap data
586 * @param v v remap data
587 * @param ker ker remap data
589 static void gaussian_kernel(float du, float dv, const XYRemap *rmap,
590 int16_t *u, int16_t *v, int16_t *ker)
595 calculate_gaussian_coeffs(du, du_coeffs);
596 calculate_gaussian_coeffs(dv, dv_coeffs);
598 for (int i = 0; i < 4; i++) {
599 for (int j = 0; j < 4; j++) {
600 u[i * 4 + j] = rmap->u[i][j];
601 v[i * 4 + j] = rmap->v[i][j];
602 ker[i * 4 + j] = lrintf(du_coeffs[j] * dv_coeffs[i] * 16385.f);
608 * Modulo operation with only positive remainders.
613 * @return positive remainder of (a / b)
615 static inline int mod(int a, int b)
617 const int res = a % b;
626 * Convert char to corresponding direction.
627 * Used for cubemap options.
629 static int get_direction(char c)
650 * Convert char to corresponding rotation angle.
651 * Used for cubemap options.
653 static int get_rotation(char c)
670 * Convert char to corresponding rotation order.
672 static int get_rorder(char c)
690 * Prepare data for processing cubemap input format.
692 * @param ctx filter context
696 static int prepare_cube_in(AVFilterContext *ctx)
698 V360Context *s = ctx->priv;
700 for (int face = 0; face < NB_FACES; face++) {
701 const char c = s->in_forder[face];
705 av_log(ctx, AV_LOG_ERROR,
706 "Incomplete in_forder option. Direction for all 6 faces should be specified.\n");
707 return AVERROR(EINVAL);
710 direction = get_direction(c);
711 if (direction == -1) {
712 av_log(ctx, AV_LOG_ERROR,
713 "Incorrect direction symbol '%c' in in_forder option.\n", c);
714 return AVERROR(EINVAL);
717 s->in_cubemap_face_order[direction] = face;
720 for (int face = 0; face < NB_FACES; face++) {
721 const char c = s->in_frot[face];
725 av_log(ctx, AV_LOG_ERROR,
726 "Incomplete in_frot option. Rotation for all 6 faces should be specified.\n");
727 return AVERROR(EINVAL);
730 rotation = get_rotation(c);
731 if (rotation == -1) {
732 av_log(ctx, AV_LOG_ERROR,
733 "Incorrect rotation symbol '%c' in in_frot option.\n", c);
734 return AVERROR(EINVAL);
737 s->in_cubemap_face_rotation[face] = rotation;
744 * Prepare data for processing cubemap output format.
746 * @param ctx filter context
750 static int prepare_cube_out(AVFilterContext *ctx)
752 V360Context *s = ctx->priv;
754 for (int face = 0; face < NB_FACES; face++) {
755 const char c = s->out_forder[face];
759 av_log(ctx, AV_LOG_ERROR,
760 "Incomplete out_forder option. Direction for all 6 faces should be specified.\n");
761 return AVERROR(EINVAL);
764 direction = get_direction(c);
765 if (direction == -1) {
766 av_log(ctx, AV_LOG_ERROR,
767 "Incorrect direction symbol '%c' in out_forder option.\n", c);
768 return AVERROR(EINVAL);
771 s->out_cubemap_direction_order[face] = direction;
774 for (int face = 0; face < NB_FACES; face++) {
775 const char c = s->out_frot[face];
779 av_log(ctx, AV_LOG_ERROR,
780 "Incomplete out_frot option. Rotation for all 6 faces should be specified.\n");
781 return AVERROR(EINVAL);
784 rotation = get_rotation(c);
785 if (rotation == -1) {
786 av_log(ctx, AV_LOG_ERROR,
787 "Incorrect rotation symbol '%c' in out_frot option.\n", c);
788 return AVERROR(EINVAL);
791 s->out_cubemap_face_rotation[face] = rotation;
797 static inline void rotate_cube_face(float *uf, float *vf, int rotation)
823 static inline void rotate_cube_face_inverse(float *uf, float *vf, int rotation)
854 static void normalize_vector(float *vec)
856 const float norm = sqrtf(vec[0] * vec[0] + vec[1] * vec[1] + vec[2] * vec[2]);
864 * Calculate 3D coordinates on sphere for corresponding cubemap position.
865 * Common operation for every cubemap.
867 * @param s filter private context
868 * @param uf horizontal cubemap coordinate [0, 1)
869 * @param vf vertical cubemap coordinate [0, 1)
870 * @param face face of cubemap
871 * @param vec coordinates on sphere
872 * @param scalew scale for uf
873 * @param scaleh scale for vf
875 static void cube_to_xyz(const V360Context *s,
876 float uf, float vf, int face,
877 float *vec, float scalew, float scaleh)
879 const int direction = s->out_cubemap_direction_order[face];
885 rotate_cube_face_inverse(&uf, &vf, s->out_cubemap_face_rotation[face]);
926 normalize_vector(vec);
930 * Calculate cubemap position for corresponding 3D coordinates on sphere.
931 * Common operation for every cubemap.
933 * @param s filter private context
934 * @param vec coordinated on sphere
935 * @param uf horizontal cubemap coordinate [0, 1)
936 * @param vf vertical cubemap coordinate [0, 1)
937 * @param direction direction of view
939 static void xyz_to_cube(const V360Context *s,
941 float *uf, float *vf, int *direction)
943 const float phi = atan2f(vec[0], -vec[2]);
944 const float theta = asinf(-vec[1]);
945 float phi_norm, theta_threshold;
948 if (phi >= -M_PI_4 && phi < M_PI_4) {
951 } else if (phi >= -(M_PI_2 + M_PI_4) && phi < -M_PI_4) {
953 phi_norm = phi + M_PI_2;
954 } else if (phi >= M_PI_4 && phi < M_PI_2 + M_PI_4) {
956 phi_norm = phi - M_PI_2;
959 phi_norm = phi + ((phi > 0.f) ? -M_PI : M_PI);
962 theta_threshold = atanf(cosf(phi_norm));
963 if (theta > theta_threshold) {
965 } else if (theta < -theta_threshold) {
969 switch (*direction) {
971 *uf = vec[2] / vec[0];
972 *vf = -vec[1] / vec[0];
975 *uf = vec[2] / vec[0];
976 *vf = vec[1] / vec[0];
979 *uf = vec[0] / vec[1];
980 *vf = -vec[2] / vec[1];
983 *uf = -vec[0] / vec[1];
984 *vf = -vec[2] / vec[1];
987 *uf = -vec[0] / vec[2];
988 *vf = vec[1] / vec[2];
991 *uf = -vec[0] / vec[2];
992 *vf = -vec[1] / vec[2];
998 face = s->in_cubemap_face_order[*direction];
999 rotate_cube_face(uf, vf, s->in_cubemap_face_rotation[face]);
1001 (*uf) *= s->input_mirror_modifier[0];
1002 (*vf) *= s->input_mirror_modifier[1];
1006 * Find position on another cube face in case of overflow/underflow.
1007 * Used for calculation of interpolation window.
1009 * @param s filter private context
1010 * @param uf horizontal cubemap coordinate
1011 * @param vf vertical cubemap coordinate
1012 * @param direction direction of view
1013 * @param new_uf new horizontal cubemap coordinate
1014 * @param new_vf new vertical cubemap coordinate
1015 * @param face face position on cubemap
1017 static void process_cube_coordinates(const V360Context *s,
1018 float uf, float vf, int direction,
1019 float *new_uf, float *new_vf, int *face)
1022 * Cubemap orientation
1029 * +-------+-------+-------+-------+ ^ e |
1031 * | left | front | right | back | | g |
1032 * +-------+-------+-------+-------+ v h v
1038 *face = s->in_cubemap_face_order[direction];
1039 rotate_cube_face_inverse(&uf, &vf, s->in_cubemap_face_rotation[*face]);
1041 if ((uf < -1.f || uf >= 1.f) && (vf < -1.f || vf >= 1.f)) {
1042 // There are no pixels to use in this case
1045 } else if (uf < -1.f) {
1047 switch (direction) {
1081 } else if (uf >= 1.f) {
1083 switch (direction) {
1117 } else if (vf < -1.f) {
1119 switch (direction) {
1153 } else if (vf >= 1.f) {
1155 switch (direction) {
1195 *face = s->in_cubemap_face_order[direction];
1196 rotate_cube_face(new_uf, new_vf, s->in_cubemap_face_rotation[*face]);
1200 * Calculate 3D coordinates on sphere for corresponding frame position in cubemap3x2 format.
1202 * @param s filter private context
1203 * @param i horizontal position on frame [0, width)
1204 * @param j vertical position on frame [0, height)
1205 * @param width frame width
1206 * @param height frame height
1207 * @param vec coordinates on sphere
1209 static int cube3x2_to_xyz(const V360Context *s,
1210 int i, int j, int width, int height,
1213 const float scalew = s->fout_pad > 0 ? 1.f - s->fout_pad / (s->out_width / 3.f) : 1.f - s->out_pad;
1214 const float scaleh = s->fout_pad > 0 ? 1.f - s->fout_pad / (s->out_height / 2.f) : 1.f - s->out_pad;
1216 const float ew = width / 3.f;
1217 const float eh = height / 2.f;
1219 const int u_face = floorf(i / ew);
1220 const int v_face = floorf(j / eh);
1221 const int face = u_face + 3 * v_face;
1223 const int u_shift = ceilf(ew * u_face);
1224 const int v_shift = ceilf(eh * v_face);
1225 const int ewi = ceilf(ew * (u_face + 1)) - u_shift;
1226 const int ehi = ceilf(eh * (v_face + 1)) - v_shift;
1228 const float uf = 2.f * (i - u_shift + 0.5f) / ewi - 1.f;
1229 const float vf = 2.f * (j - v_shift + 0.5f) / ehi - 1.f;
1231 cube_to_xyz(s, uf, vf, face, vec, scalew, scaleh);
1237 * Calculate frame position in cubemap3x2 format for corresponding 3D coordinates on sphere.
1239 * @param s filter private context
1240 * @param vec coordinates on sphere
1241 * @param width frame width
1242 * @param height frame height
1243 * @param us horizontal coordinates for interpolation window
1244 * @param vs vertical coordinates for interpolation window
1245 * @param du horizontal relative coordinate
1246 * @param dv vertical relative coordinate
1248 static int xyz_to_cube3x2(const V360Context *s,
1249 const float *vec, int width, int height,
1250 int16_t us[4][4], int16_t vs[4][4], float *du, float *dv)
1252 const float scalew = s->fin_pad > 0 ? 1.f - s->fin_pad / (s->in_width / 3.f) : 1.f - s->in_pad;
1253 const float scaleh = s->fin_pad > 0 ? 1.f - s->fin_pad / (s->in_height / 2.f) : 1.f - s->in_pad;
1254 const float ew = width / 3.f;
1255 const float eh = height / 2.f;
1259 int direction, face;
1262 xyz_to_cube(s, vec, &uf, &vf, &direction);
1267 face = s->in_cubemap_face_order[direction];
1270 ewi = ceilf(ew * (u_face + 1)) - ceilf(ew * u_face);
1271 ehi = ceilf(eh * (v_face + 1)) - ceilf(eh * v_face);
1273 uf = 0.5f * ewi * (uf + 1.f) - 0.5f;
1274 vf = 0.5f * ehi * (vf + 1.f) - 0.5f;
1282 for (int i = -1; i < 3; i++) {
1283 for (int j = -1; j < 3; j++) {
1284 int new_ui = ui + j;
1285 int new_vi = vi + i;
1286 int u_shift, v_shift;
1287 int new_ewi, new_ehi;
1289 if (new_ui >= 0 && new_ui < ewi && new_vi >= 0 && new_vi < ehi) {
1290 face = s->in_cubemap_face_order[direction];
1294 u_shift = ceilf(ew * u_face);
1295 v_shift = ceilf(eh * v_face);
1297 uf = 2.f * new_ui / ewi - 1.f;
1298 vf = 2.f * new_vi / ehi - 1.f;
1303 process_cube_coordinates(s, uf, vf, direction, &uf, &vf, &face);
1310 u_shift = ceilf(ew * u_face);
1311 v_shift = ceilf(eh * v_face);
1312 new_ewi = ceilf(ew * (u_face + 1)) - u_shift;
1313 new_ehi = ceilf(eh * (v_face + 1)) - v_shift;
1315 new_ui = av_clip(lrintf(0.5f * new_ewi * (uf + 1.f)), 0, new_ewi - 1);
1316 new_vi = av_clip(lrintf(0.5f * new_ehi * (vf + 1.f)), 0, new_ehi - 1);
1319 us[i + 1][j + 1] = u_shift + new_ui;
1320 vs[i + 1][j + 1] = v_shift + new_vi;
1328 * Calculate 3D coordinates on sphere for corresponding frame position in cubemap1x6 format.
1330 * @param s filter private context
1331 * @param i horizontal position on frame [0, width)
1332 * @param j vertical position on frame [0, height)
1333 * @param width frame width
1334 * @param height frame height
1335 * @param vec coordinates on sphere
1337 static int cube1x6_to_xyz(const V360Context *s,
1338 int i, int j, int width, int height,
1341 const float scalew = s->fout_pad > 0 ? 1.f - (float)(s->fout_pad) / s->out_width : 1.f - s->out_pad;
1342 const float scaleh = s->fout_pad > 0 ? 1.f - s->fout_pad / (s->out_height / 6.f) : 1.f - s->out_pad;
1344 const float ew = width;
1345 const float eh = height / 6.f;
1347 const int face = floorf(j / eh);
1349 const int v_shift = ceilf(eh * face);
1350 const int ehi = ceilf(eh * (face + 1)) - v_shift;
1352 const float uf = 2.f * (i + 0.5f) / ew - 1.f;
1353 const float vf = 2.f * (j - v_shift + 0.5f) / ehi - 1.f;
1355 cube_to_xyz(s, uf, vf, face, vec, scalew, scaleh);
1361 * Calculate 3D coordinates on sphere for corresponding frame position in cubemap6x1 format.
1363 * @param s filter private context
1364 * @param i horizontal position on frame [0, width)
1365 * @param j vertical position on frame [0, height)
1366 * @param width frame width
1367 * @param height frame height
1368 * @param vec coordinates on sphere
1370 static int cube6x1_to_xyz(const V360Context *s,
1371 int i, int j, int width, int height,
1374 const float scalew = s->fout_pad > 0 ? 1.f - s->fout_pad / (s->out_width / 6.f) : 1.f - s->out_pad;
1375 const float scaleh = s->fout_pad > 0 ? 1.f - (float)(s->fout_pad) / s->out_height : 1.f - s->out_pad;
1377 const float ew = width / 6.f;
1378 const float eh = height;
1380 const int face = floorf(i / ew);
1382 const int u_shift = ceilf(ew * face);
1383 const int ewi = ceilf(ew * (face + 1)) - u_shift;
1385 const float uf = 2.f * (i - u_shift + 0.5f) / ewi - 1.f;
1386 const float vf = 2.f * (j + 0.5f) / eh - 1.f;
1388 cube_to_xyz(s, uf, vf, face, vec, scalew, scaleh);
1394 * Calculate frame position in cubemap1x6 format for corresponding 3D coordinates on sphere.
1396 * @param s filter private context
1397 * @param vec coordinates on sphere
1398 * @param width frame width
1399 * @param height frame height
1400 * @param us horizontal coordinates for interpolation window
1401 * @param vs vertical coordinates for interpolation window
1402 * @param du horizontal relative coordinate
1403 * @param dv vertical relative coordinate
1405 static int xyz_to_cube1x6(const V360Context *s,
1406 const float *vec, int width, int height,
1407 int16_t us[4][4], int16_t vs[4][4], float *du, float *dv)
1409 const float scalew = s->fin_pad > 0 ? 1.f - (float)(s->fin_pad) / s->in_width : 1.f - s->in_pad;
1410 const float scaleh = s->fin_pad > 0 ? 1.f - s->fin_pad / (s->in_height / 6.f) : 1.f - s->in_pad;
1411 const float eh = height / 6.f;
1412 const int ewi = width;
1416 int direction, face;
1418 xyz_to_cube(s, vec, &uf, &vf, &direction);
1423 face = s->in_cubemap_face_order[direction];
1424 ehi = ceilf(eh * (face + 1)) - ceilf(eh * face);
1426 uf = 0.5f * ewi * (uf + 1.f) - 0.5f;
1427 vf = 0.5f * ehi * (vf + 1.f) - 0.5f;
1435 for (int i = -1; i < 3; i++) {
1436 for (int j = -1; j < 3; j++) {
1437 int new_ui = ui + j;
1438 int new_vi = vi + i;
1442 if (new_ui >= 0 && new_ui < ewi && new_vi >= 0 && new_vi < ehi) {
1443 face = s->in_cubemap_face_order[direction];
1445 v_shift = ceilf(eh * face);
1447 uf = 2.f * new_ui / ewi - 1.f;
1448 vf = 2.f * new_vi / ehi - 1.f;
1453 process_cube_coordinates(s, uf, vf, direction, &uf, &vf, &face);
1458 v_shift = ceilf(eh * face);
1459 new_ehi = ceilf(eh * (face + 1)) - v_shift;
1461 new_ui = av_clip(lrintf(0.5f * ewi * (uf + 1.f)), 0, ewi - 1);
1462 new_vi = av_clip(lrintf(0.5f * new_ehi * (vf + 1.f)), 0, new_ehi - 1);
1465 us[i + 1][j + 1] = new_ui;
1466 vs[i + 1][j + 1] = v_shift + new_vi;
1474 * Calculate frame position in cubemap6x1 format for corresponding 3D coordinates on sphere.
1476 * @param s filter private context
1477 * @param vec coordinates on sphere
1478 * @param width frame width
1479 * @param height frame height
1480 * @param us horizontal coordinates for interpolation window
1481 * @param vs vertical coordinates for interpolation window
1482 * @param du horizontal relative coordinate
1483 * @param dv vertical relative coordinate
1485 static int xyz_to_cube6x1(const V360Context *s,
1486 const float *vec, int width, int height,
1487 int16_t us[4][4], int16_t vs[4][4], float *du, float *dv)
1489 const float scalew = s->fin_pad > 0 ? 1.f - s->fin_pad / (s->in_width / 6.f) : 1.f - s->in_pad;
1490 const float scaleh = s->fin_pad > 0 ? 1.f - (float)(s->fin_pad) / s->in_height : 1.f - s->in_pad;
1491 const float ew = width / 6.f;
1492 const int ehi = height;
1496 int direction, face;
1498 xyz_to_cube(s, vec, &uf, &vf, &direction);
1503 face = s->in_cubemap_face_order[direction];
1504 ewi = ceilf(ew * (face + 1)) - ceilf(ew * face);
1506 uf = 0.5f * ewi * (uf + 1.f) - 0.5f;
1507 vf = 0.5f * ehi * (vf + 1.f) - 0.5f;
1515 for (int i = -1; i < 3; i++) {
1516 for (int j = -1; j < 3; j++) {
1517 int new_ui = ui + j;
1518 int new_vi = vi + i;
1522 if (new_ui >= 0 && new_ui < ewi && new_vi >= 0 && new_vi < ehi) {
1523 face = s->in_cubemap_face_order[direction];
1525 u_shift = ceilf(ew * face);
1527 uf = 2.f * new_ui / ewi - 1.f;
1528 vf = 2.f * new_vi / ehi - 1.f;
1533 process_cube_coordinates(s, uf, vf, direction, &uf, &vf, &face);
1538 u_shift = ceilf(ew * face);
1539 new_ewi = ceilf(ew * (face + 1)) - u_shift;
1541 new_ui = av_clip(lrintf(0.5f * new_ewi * (uf + 1.f)), 0, new_ewi - 1);
1542 new_vi = av_clip(lrintf(0.5f * ehi * (vf + 1.f)), 0, ehi - 1);
1545 us[i + 1][j + 1] = u_shift + new_ui;
1546 vs[i + 1][j + 1] = new_vi;
1554 * Calculate 3D coordinates on sphere for corresponding frame position in equirectangular format.
1556 * @param s filter private context
1557 * @param i horizontal position on frame [0, width)
1558 * @param j vertical position on frame [0, height)
1559 * @param width frame width
1560 * @param height frame height
1561 * @param vec coordinates on sphere
1563 static int equirect_to_xyz(const V360Context *s,
1564 int i, int j, int width, int height,
1567 const float phi = ((2.f * i + 0.5f) / width - 1.f) * M_PI;
1568 const float theta = ((2.f * j + 0.5f) / height - 1.f) * M_PI_2;
1570 const float sin_phi = sinf(phi);
1571 const float cos_phi = cosf(phi);
1572 const float sin_theta = sinf(theta);
1573 const float cos_theta = cosf(theta);
1575 vec[0] = cos_theta * sin_phi;
1576 vec[1] = -sin_theta;
1577 vec[2] = -cos_theta * cos_phi;
1583 * Prepare data for processing stereographic output format.
1585 * @param ctx filter context
1587 * @return error code
1589 static int prepare_stereographic_out(AVFilterContext *ctx)
1591 V360Context *s = ctx->priv;
1593 s->flat_range[0] = tanf(FFMIN(s->h_fov, 359.f) * M_PI / 720.f);
1594 s->flat_range[1] = tanf(FFMIN(s->v_fov, 359.f) * M_PI / 720.f);
1600 * Calculate 3D coordinates on sphere for corresponding frame position in stereographic format.
1602 * @param s filter private context
1603 * @param i horizontal position on frame [0, width)
1604 * @param j vertical position on frame [0, height)
1605 * @param width frame width
1606 * @param height frame height
1607 * @param vec coordinates on sphere
1609 static int stereographic_to_xyz(const V360Context *s,
1610 int i, int j, int width, int height,
1613 const float x = ((2.f * i) / width - 1.f) * s->flat_range[0];
1614 const float y = ((2.f * j) / height - 1.f) * s->flat_range[1];
1615 const float xy = x * x + y * y;
1617 vec[0] = 2.f * x / (1.f + xy);
1618 vec[1] = (-1.f + xy) / (1.f + xy);
1619 vec[2] = 2.f * y / (1.f + xy);
1621 normalize_vector(vec);
1627 * Prepare data for processing stereographic input format.
1629 * @param ctx filter context
1631 * @return error code
1633 static int prepare_stereographic_in(AVFilterContext *ctx)
1635 V360Context *s = ctx->priv;
1637 s->iflat_range[0] = tanf(FFMIN(s->ih_fov, 359.f) * M_PI / 720.f);
1638 s->iflat_range[1] = tanf(FFMIN(s->iv_fov, 359.f) * M_PI / 720.f);
1644 * Calculate frame position in stereographic format for corresponding 3D coordinates on sphere.
1646 * @param s filter private context
1647 * @param vec coordinates on sphere
1648 * @param width frame width
1649 * @param height frame height
1650 * @param us horizontal coordinates for interpolation window
1651 * @param vs vertical coordinates for interpolation window
1652 * @param du horizontal relative coordinate
1653 * @param dv vertical relative coordinate
1655 static int xyz_to_stereographic(const V360Context *s,
1656 const float *vec, int width, int height,
1657 int16_t us[4][4], int16_t vs[4][4], float *du, float *dv)
1659 const float x = vec[0] / (1.f - vec[1]) / s->iflat_range[0] * s->input_mirror_modifier[0];
1660 const float y = vec[2] / (1.f - vec[1]) / s->iflat_range[1] * s->input_mirror_modifier[1];
1662 int visible, ui, vi;
1664 uf = (x + 1.f) * width / 2.f;
1665 vf = (y + 1.f) * height / 2.f;
1669 visible = isfinite(x) && isfinite(y) && vi >= 0 && vi < height && ui >= 0 && ui < width;
1671 *du = visible ? uf - ui : 0.f;
1672 *dv = visible ? vf - vi : 0.f;
1674 for (int i = -1; i < 3; i++) {
1675 for (int j = -1; j < 3; j++) {
1676 us[i + 1][j + 1] = visible ? av_clip(ui + j, 0, width - 1) : 0;
1677 vs[i + 1][j + 1] = visible ? av_clip(vi + i, 0, height - 1) : 0;
1685 * Calculate frame position in equirectangular format for corresponding 3D coordinates on sphere.
1687 * @param s filter private context
1688 * @param vec coordinates on sphere
1689 * @param width frame width
1690 * @param height frame height
1691 * @param us horizontal coordinates for interpolation window
1692 * @param vs vertical coordinates for interpolation window
1693 * @param du horizontal relative coordinate
1694 * @param dv vertical relative coordinate
1696 static int xyz_to_equirect(const V360Context *s,
1697 const float *vec, int width, int height,
1698 int16_t us[4][4], int16_t vs[4][4], float *du, float *dv)
1700 const float phi = atan2f(vec[0], -vec[2]) * s->input_mirror_modifier[0];
1701 const float theta = asinf(-vec[1]) * s->input_mirror_modifier[1];
1705 uf = (phi / M_PI + 1.f) * width / 2.f;
1706 vf = (theta / M_PI_2 + 1.f) * height / 2.f;
1713 for (int i = -1; i < 3; i++) {
1714 for (int j = -1; j < 3; j++) {
1715 us[i + 1][j + 1] = mod(ui + j, width);
1716 vs[i + 1][j + 1] = av_clip(vi + i, 0, height - 1);
1724 * Prepare data for processing flat input format.
1726 * @param ctx filter context
1728 * @return error code
1730 static int prepare_flat_in(AVFilterContext *ctx)
1732 V360Context *s = ctx->priv;
1734 s->iflat_range[0] = tanf(0.5f * s->ih_fov * M_PI / 180.f);
1735 s->iflat_range[1] = tanf(0.5f * s->iv_fov * M_PI / 180.f);
1741 * Calculate frame position in flat format for corresponding 3D coordinates on sphere.
1743 * @param s filter private context
1744 * @param vec coordinates on sphere
1745 * @param width frame width
1746 * @param height frame height
1747 * @param us horizontal coordinates for interpolation window
1748 * @param vs vertical coordinates for interpolation window
1749 * @param du horizontal relative coordinate
1750 * @param dv vertical relative coordinate
1752 static int xyz_to_flat(const V360Context *s,
1753 const float *vec, int width, int height,
1754 int16_t us[4][4], int16_t vs[4][4], float *du, float *dv)
1756 const float theta = acosf(vec[2]);
1757 const float r = tanf(theta);
1758 const float rr = fabsf(r) < 1e+6f ? r : hypotf(width, height);
1759 const float zf = -vec[2];
1760 const float h = hypotf(vec[0], vec[1]);
1761 const float c = h <= 1e-6f ? 1.f : rr / h;
1762 float uf = -vec[0] * c / s->iflat_range[0] * s->input_mirror_modifier[0];
1763 float vf = vec[1] * c / s->iflat_range[1] * s->input_mirror_modifier[1];
1764 int visible, ui, vi;
1766 uf = zf >= 0.f ? (uf + 1.f) * width / 2.f : 0.f;
1767 vf = zf >= 0.f ? (vf + 1.f) * height / 2.f : 0.f;
1772 visible = vi >= 0 && vi < height && ui >= 0 && ui < width && zf >= 0.f;
1777 for (int i = -1; i < 3; i++) {
1778 for (int j = -1; j < 3; j++) {
1779 us[i + 1][j + 1] = visible ? av_clip(ui + j, 0, width - 1) : 0;
1780 vs[i + 1][j + 1] = visible ? av_clip(vi + i, 0, height - 1) : 0;
1788 * Calculate frame position in mercator format for corresponding 3D coordinates on sphere.
1790 * @param s filter private context
1791 * @param vec coordinates on sphere
1792 * @param width frame width
1793 * @param height frame height
1794 * @param us horizontal coordinates for interpolation window
1795 * @param vs vertical coordinates for interpolation window
1796 * @param du horizontal relative coordinate
1797 * @param dv vertical relative coordinate
1799 static int xyz_to_mercator(const V360Context *s,
1800 const float *vec, int width, int height,
1801 int16_t us[4][4], int16_t vs[4][4], float *du, float *dv)
1803 const float phi = atan2f(vec[0], -vec[2]) * s->input_mirror_modifier[0];
1804 const float theta = -vec[1] * s->input_mirror_modifier[1];
1808 uf = (phi / M_PI + 1.f) * width / 2.f;
1809 vf = (av_clipf(logf((1.f + theta) / (1.f - theta)) / (2.f * M_PI), -1.f, 1.f) + 1.f) * height / 2.f;
1816 for (int i = -1; i < 3; i++) {
1817 for (int j = -1; j < 3; j++) {
1818 us[i + 1][j + 1] = av_clip(ui + j, 0, width - 1);
1819 vs[i + 1][j + 1] = av_clip(vi + i, 0, height - 1);
1827 * Calculate 3D coordinates on sphere for corresponding frame position in mercator format.
1829 * @param s filter private context
1830 * @param i horizontal position on frame [0, width)
1831 * @param j vertical position on frame [0, height)
1832 * @param width frame width
1833 * @param height frame height
1834 * @param vec coordinates on sphere
1836 static int mercator_to_xyz(const V360Context *s,
1837 int i, int j, int width, int height,
1840 const float phi = ((2.f * i) / width - 1.f) * M_PI + M_PI_2;
1841 const float y = ((2.f * j) / height - 1.f) * M_PI;
1842 const float div = expf(2.f * y) + 1.f;
1844 const float sin_phi = sinf(phi);
1845 const float cos_phi = cosf(phi);
1846 const float sin_theta = -2.f * expf(y) / div;
1847 const float cos_theta = -(expf(2.f * y) - 1.f) / div;
1849 vec[0] = sin_theta * cos_phi;
1851 vec[2] = sin_theta * sin_phi;
1857 * Calculate frame position in ball format for corresponding 3D coordinates on sphere.
1859 * @param s filter private context
1860 * @param vec coordinates on sphere
1861 * @param width frame width
1862 * @param height frame height
1863 * @param us horizontal coordinates for interpolation window
1864 * @param vs vertical coordinates for interpolation window
1865 * @param du horizontal relative coordinate
1866 * @param dv vertical relative coordinate
1868 static int xyz_to_ball(const V360Context *s,
1869 const float *vec, int width, int height,
1870 int16_t us[4][4], int16_t vs[4][4], float *du, float *dv)
1872 const float l = hypotf(vec[0], vec[1]);
1873 const float r = sqrtf(1.f + vec[2]) / M_SQRT2;
1877 uf = (1.f + r * vec[0] * s->input_mirror_modifier[0] / (l > 0.f ? l : 1.f)) * width * 0.5f;
1878 vf = (1.f - r * vec[1] * s->input_mirror_modifier[1] / (l > 0.f ? l : 1.f)) * height * 0.5f;
1886 for (int i = -1; i < 3; i++) {
1887 for (int j = -1; j < 3; j++) {
1888 us[i + 1][j + 1] = av_clip(ui + j, 0, width - 1);
1889 vs[i + 1][j + 1] = av_clip(vi + i, 0, height - 1);
1897 * Calculate 3D coordinates on sphere for corresponding frame position in ball format.
1899 * @param s filter private context
1900 * @param i horizontal position on frame [0, width)
1901 * @param j vertical position on frame [0, height)
1902 * @param width frame width
1903 * @param height frame height
1904 * @param vec coordinates on sphere
1906 static int ball_to_xyz(const V360Context *s,
1907 int i, int j, int width, int height,
1910 const float x = (2.f * i) / width - 1.f;
1911 const float y = (2.f * j) / height - 1.f;
1912 const float l = hypotf(x, y);
1915 const float z = 2.f * l * sqrtf(1.f - l * l);
1917 vec[0] = z * x / (l > 0.f ? l : 1.f);
1918 vec[1] = -z * y / (l > 0.f ? l : 1.f);
1919 vec[2] = -1.f + 2.f * l * l;
1931 * Calculate 3D coordinates on sphere for corresponding frame position in hammer format.
1933 * @param s filter private context
1934 * @param i horizontal position on frame [0, width)
1935 * @param j vertical position on frame [0, height)
1936 * @param width frame width
1937 * @param height frame height
1938 * @param vec coordinates on sphere
1940 static int hammer_to_xyz(const V360Context *s,
1941 int i, int j, int width, int height,
1944 const float x = ((2.f * i) / width - 1.f);
1945 const float y = ((2.f * j) / height - 1.f);
1947 const float xx = x * x;
1948 const float yy = y * y;
1950 const float z = sqrtf(1.f - xx * 0.5f - yy * 0.5f);
1952 const float a = M_SQRT2 * x * z;
1953 const float b = 2.f * z * z - 1.f;
1955 const float aa = a * a;
1956 const float bb = b * b;
1958 const float w = sqrtf(1.f - 2.f * yy * z * z);
1960 vec[0] = w * 2.f * a * b / (aa + bb);
1961 vec[1] = -M_SQRT2 * y * z;
1962 vec[2] = -w * (bb - aa) / (aa + bb);
1964 normalize_vector(vec);
1970 * Calculate frame position in hammer format for corresponding 3D coordinates on sphere.
1972 * @param s filter private context
1973 * @param vec coordinates on sphere
1974 * @param width frame width
1975 * @param height frame height
1976 * @param us horizontal coordinates for interpolation window
1977 * @param vs vertical coordinates for interpolation window
1978 * @param du horizontal relative coordinate
1979 * @param dv vertical relative coordinate
1981 static int xyz_to_hammer(const V360Context *s,
1982 const float *vec, int width, int height,
1983 int16_t us[4][4], int16_t vs[4][4], float *du, float *dv)
1985 const float theta = atan2f(vec[0], -vec[2]) * s->input_mirror_modifier[0];
1987 const float z = sqrtf(1.f + sqrtf(1.f - vec[1] * vec[1]) * cosf(theta * 0.5f));
1988 const float x = sqrtf(1.f - vec[1] * vec[1]) * sinf(theta * 0.5f) / z;
1989 const float y = -vec[1] / z * s->input_mirror_modifier[1];
1993 uf = (x + 1.f) * width / 2.f;
1994 vf = (y + 1.f) * height / 2.f;
2001 for (int i = -1; i < 3; i++) {
2002 for (int j = -1; j < 3; j++) {
2003 us[i + 1][j + 1] = av_clip(ui + j, 0, width - 1);
2004 vs[i + 1][j + 1] = av_clip(vi + i, 0, height - 1);
2012 * Calculate 3D coordinates on sphere for corresponding frame position in sinusoidal format.
2014 * @param s filter private context
2015 * @param i horizontal position on frame [0, width)
2016 * @param j vertical position on frame [0, height)
2017 * @param width frame width
2018 * @param height frame height
2019 * @param vec coordinates on sphere
2021 static int sinusoidal_to_xyz(const V360Context *s,
2022 int i, int j, int width, int height,
2025 const float theta = ((2.f * j) / height - 1.f) * M_PI_2;
2026 const float phi = ((2.f * i) / width - 1.f) * M_PI / cosf(theta);
2028 const float sin_phi = sinf(phi);
2029 const float cos_phi = cosf(phi);
2030 const float sin_theta = sinf(theta);
2031 const float cos_theta = cosf(theta);
2033 vec[0] = cos_theta * sin_phi;
2034 vec[1] = -sin_theta;
2035 vec[2] = -cos_theta * cos_phi;
2037 normalize_vector(vec);
2043 * Calculate frame position in sinusoidal format for corresponding 3D coordinates on sphere.
2045 * @param s filter private context
2046 * @param vec coordinates on sphere
2047 * @param width frame width
2048 * @param height frame height
2049 * @param us horizontal coordinates for interpolation window
2050 * @param vs vertical coordinates for interpolation window
2051 * @param du horizontal relative coordinate
2052 * @param dv vertical relative coordinate
2054 static int xyz_to_sinusoidal(const V360Context *s,
2055 const float *vec, int width, int height,
2056 int16_t us[4][4], int16_t vs[4][4], float *du, float *dv)
2058 const float theta = asinf(-vec[1]) * s->input_mirror_modifier[1];
2059 const float phi = atan2f(vec[0], -vec[2]) * s->input_mirror_modifier[0] * cosf(theta);
2063 uf = (phi / M_PI + 1.f) * width / 2.f;
2064 vf = (theta / M_PI_2 + 1.f) * height / 2.f;
2071 for (int i = -1; i < 3; i++) {
2072 for (int j = -1; j < 3; j++) {
2073 us[i + 1][j + 1] = av_clip(ui + j, 0, width - 1);
2074 vs[i + 1][j + 1] = av_clip(vi + i, 0, height - 1);
2082 * Prepare data for processing equi-angular cubemap input format.
2084 * @param ctx filter context
2086 * @return error code
2088 static int prepare_eac_in(AVFilterContext *ctx)
2090 V360Context *s = ctx->priv;
2092 if (s->ih_flip && s->iv_flip) {
2093 s->in_cubemap_face_order[RIGHT] = BOTTOM_LEFT;
2094 s->in_cubemap_face_order[LEFT] = BOTTOM_RIGHT;
2095 s->in_cubemap_face_order[UP] = TOP_LEFT;
2096 s->in_cubemap_face_order[DOWN] = TOP_RIGHT;
2097 s->in_cubemap_face_order[FRONT] = BOTTOM_MIDDLE;
2098 s->in_cubemap_face_order[BACK] = TOP_MIDDLE;
2099 } else if (s->ih_flip) {
2100 s->in_cubemap_face_order[RIGHT] = TOP_LEFT;
2101 s->in_cubemap_face_order[LEFT] = TOP_RIGHT;
2102 s->in_cubemap_face_order[UP] = BOTTOM_LEFT;
2103 s->in_cubemap_face_order[DOWN] = BOTTOM_RIGHT;
2104 s->in_cubemap_face_order[FRONT] = TOP_MIDDLE;
2105 s->in_cubemap_face_order[BACK] = BOTTOM_MIDDLE;
2106 } else if (s->iv_flip) {
2107 s->in_cubemap_face_order[RIGHT] = BOTTOM_RIGHT;
2108 s->in_cubemap_face_order[LEFT] = BOTTOM_LEFT;
2109 s->in_cubemap_face_order[UP] = TOP_RIGHT;
2110 s->in_cubemap_face_order[DOWN] = TOP_LEFT;
2111 s->in_cubemap_face_order[FRONT] = BOTTOM_MIDDLE;
2112 s->in_cubemap_face_order[BACK] = TOP_MIDDLE;
2114 s->in_cubemap_face_order[RIGHT] = TOP_RIGHT;
2115 s->in_cubemap_face_order[LEFT] = TOP_LEFT;
2116 s->in_cubemap_face_order[UP] = BOTTOM_RIGHT;
2117 s->in_cubemap_face_order[DOWN] = BOTTOM_LEFT;
2118 s->in_cubemap_face_order[FRONT] = TOP_MIDDLE;
2119 s->in_cubemap_face_order[BACK] = BOTTOM_MIDDLE;
2123 s->in_cubemap_face_rotation[TOP_LEFT] = ROT_270;
2124 s->in_cubemap_face_rotation[TOP_MIDDLE] = ROT_90;
2125 s->in_cubemap_face_rotation[TOP_RIGHT] = ROT_270;
2126 s->in_cubemap_face_rotation[BOTTOM_LEFT] = ROT_0;
2127 s->in_cubemap_face_rotation[BOTTOM_MIDDLE] = ROT_0;
2128 s->in_cubemap_face_rotation[BOTTOM_RIGHT] = ROT_0;
2130 s->in_cubemap_face_rotation[TOP_LEFT] = ROT_0;
2131 s->in_cubemap_face_rotation[TOP_MIDDLE] = ROT_0;
2132 s->in_cubemap_face_rotation[TOP_RIGHT] = ROT_0;
2133 s->in_cubemap_face_rotation[BOTTOM_LEFT] = ROT_270;
2134 s->in_cubemap_face_rotation[BOTTOM_MIDDLE] = ROT_90;
2135 s->in_cubemap_face_rotation[BOTTOM_RIGHT] = ROT_270;
2142 * Prepare data for processing equi-angular cubemap output format.
2144 * @param ctx filter context
2146 * @return error code
2148 static int prepare_eac_out(AVFilterContext *ctx)
2150 V360Context *s = ctx->priv;
2152 s->out_cubemap_direction_order[TOP_LEFT] = LEFT;
2153 s->out_cubemap_direction_order[TOP_MIDDLE] = FRONT;
2154 s->out_cubemap_direction_order[TOP_RIGHT] = RIGHT;
2155 s->out_cubemap_direction_order[BOTTOM_LEFT] = DOWN;
2156 s->out_cubemap_direction_order[BOTTOM_MIDDLE] = BACK;
2157 s->out_cubemap_direction_order[BOTTOM_RIGHT] = UP;
2159 s->out_cubemap_face_rotation[TOP_LEFT] = ROT_0;
2160 s->out_cubemap_face_rotation[TOP_MIDDLE] = ROT_0;
2161 s->out_cubemap_face_rotation[TOP_RIGHT] = ROT_0;
2162 s->out_cubemap_face_rotation[BOTTOM_LEFT] = ROT_270;
2163 s->out_cubemap_face_rotation[BOTTOM_MIDDLE] = ROT_90;
2164 s->out_cubemap_face_rotation[BOTTOM_RIGHT] = ROT_270;
2170 * Calculate 3D coordinates on sphere for corresponding frame position in equi-angular cubemap format.
2172 * @param s filter private context
2173 * @param i horizontal position on frame [0, width)
2174 * @param j vertical position on frame [0, height)
2175 * @param width frame width
2176 * @param height frame height
2177 * @param vec coordinates on sphere
2179 static int eac_to_xyz(const V360Context *s,
2180 int i, int j, int width, int height,
2183 const float pixel_pad = 2;
2184 const float u_pad = pixel_pad / width;
2185 const float v_pad = pixel_pad / height;
2187 int u_face, v_face, face;
2189 float l_x, l_y, l_z;
2191 float uf = (i + 0.5f) / width;
2192 float vf = (j + 0.5f) / height;
2194 // EAC has 2-pixel padding on faces except between faces on the same row
2195 // Padding pixels seems not to be stretched with tangent as regular pixels
2196 // Formulas below approximate original padding as close as I could get experimentally
2198 // Horizontal padding
2199 uf = 3.f * (uf - u_pad) / (1.f - 2.f * u_pad);
2203 } else if (uf >= 3.f) {
2207 u_face = floorf(uf);
2208 uf = fmodf(uf, 1.f) - 0.5f;
2212 v_face = floorf(vf * 2.f);
2213 vf = (vf - v_pad - 0.5f * v_face) / (0.5f - 2.f * v_pad) - 0.5f;
2215 if (uf >= -0.5f && uf < 0.5f) {
2216 uf = tanf(M_PI_2 * uf);
2220 if (vf >= -0.5f && vf < 0.5f) {
2221 vf = tanf(M_PI_2 * vf);
2226 face = u_face + 3 * v_face;
2267 normalize_vector(vec);
2273 * Calculate frame position in equi-angular cubemap format for corresponding 3D coordinates on sphere.
2275 * @param s filter private context
2276 * @param vec coordinates on sphere
2277 * @param width frame width
2278 * @param height frame height
2279 * @param us horizontal coordinates for interpolation window
2280 * @param vs vertical coordinates for interpolation window
2281 * @param du horizontal relative coordinate
2282 * @param dv vertical relative coordinate
2284 static int xyz_to_eac(const V360Context *s,
2285 const float *vec, int width, int height,
2286 int16_t us[4][4], int16_t vs[4][4], float *du, float *dv)
2288 const float pixel_pad = 2;
2289 const float u_pad = pixel_pad / width;
2290 const float v_pad = pixel_pad / height;
2294 int direction, face;
2297 xyz_to_cube(s, vec, &uf, &vf, &direction);
2299 face = s->in_cubemap_face_order[direction];
2303 uf = M_2_PI * atanf(uf) + 0.5f;
2304 vf = M_2_PI * atanf(vf) + 0.5f;
2306 // These formulas are inversed from eac_to_xyz ones
2307 uf = (uf + u_face) * (1.f - 2.f * u_pad) / 3.f + u_pad;
2308 vf = vf * (0.5f - 2.f * v_pad) + v_pad + 0.5f * v_face;
2322 for (int i = -1; i < 3; i++) {
2323 for (int j = -1; j < 3; j++) {
2324 us[i + 1][j + 1] = av_clip(ui + j, 0, width - 1);
2325 vs[i + 1][j + 1] = av_clip(vi + i, 0, height - 1);
2333 * Prepare data for processing flat output format.
2335 * @param ctx filter context
2337 * @return error code
2339 static int prepare_flat_out(AVFilterContext *ctx)
2341 V360Context *s = ctx->priv;
2343 s->flat_range[0] = tanf(0.5f * s->h_fov * M_PI / 180.f);
2344 s->flat_range[1] = tanf(0.5f * s->v_fov * M_PI / 180.f);
2350 * Calculate 3D coordinates on sphere for corresponding frame position in flat format.
2352 * @param s filter private context
2353 * @param i horizontal position on frame [0, width)
2354 * @param j vertical position on frame [0, height)
2355 * @param width frame width
2356 * @param height frame height
2357 * @param vec coordinates on sphere
2359 static int flat_to_xyz(const V360Context *s,
2360 int i, int j, int width, int height,
2363 const float l_x = s->flat_range[0] * (2.f * i / width - 1.f);
2364 const float l_y = -s->flat_range[1] * (2.f * j / height - 1.f);
2370 normalize_vector(vec);
2376 * Prepare data for processing fisheye output format.
2378 * @param ctx filter context
2380 * @return error code
2382 static int prepare_fisheye_out(AVFilterContext *ctx)
2384 V360Context *s = ctx->priv;
2386 s->flat_range[0] = s->h_fov / 180.f;
2387 s->flat_range[1] = s->v_fov / 180.f;
2393 * Calculate 3D coordinates on sphere for corresponding frame position in fisheye format.
2395 * @param s filter private context
2396 * @param i horizontal position on frame [0, width)
2397 * @param j vertical position on frame [0, height)
2398 * @param width frame width
2399 * @param height frame height
2400 * @param vec coordinates on sphere
2402 static int fisheye_to_xyz(const V360Context *s,
2403 int i, int j, int width, int height,
2406 const float uf = s->flat_range[0] * ((2.f * i) / width - 1.f);
2407 const float vf = s->flat_range[1] * ((2.f * j + 1.f) / height - 1.f);
2409 const float phi = -atan2f(vf, uf);
2410 const float theta = -M_PI_2 * (1.f - hypotf(uf, vf));
2412 vec[0] = cosf(theta) * cosf(phi);
2413 vec[1] = cosf(theta) * sinf(phi);
2414 vec[2] = sinf(theta);
2416 normalize_vector(vec);
2422 * Prepare data for processing fisheye input format.
2424 * @param ctx filter context
2426 * @return error code
2428 static int prepare_fisheye_in(AVFilterContext *ctx)
2430 V360Context *s = ctx->priv;
2432 s->iflat_range[0] = s->ih_fov / 180.f;
2433 s->iflat_range[1] = s->iv_fov / 180.f;
2439 * Calculate frame position in fisheye format for corresponding 3D coordinates on sphere.
2441 * @param s filter private context
2442 * @param vec coordinates on sphere
2443 * @param width frame width
2444 * @param height frame height
2445 * @param us horizontal coordinates for interpolation window
2446 * @param vs vertical coordinates for interpolation window
2447 * @param du horizontal relative coordinate
2448 * @param dv vertical relative coordinate
2450 static int xyz_to_fisheye(const V360Context *s,
2451 const float *vec, int width, int height,
2452 int16_t us[4][4], int16_t vs[4][4], float *du, float *dv)
2454 const float phi = -atan2f(hypotf(vec[0], vec[1]), -vec[2]) / M_PI;
2455 const float theta = -atan2f(vec[0], vec[1]);
2457 float uf = sinf(theta) * phi * s->input_mirror_modifier[0] / s->iflat_range[0];
2458 float vf = cosf(theta) * phi * s->input_mirror_modifier[1] / s->iflat_range[1];
2460 const int visible = hypotf(uf, vf) <= 0.5f;
2463 uf = (uf + 0.5f) * width;
2464 vf = (vf + 0.5f) * height;
2469 *du = visible ? uf - ui : 0.f;
2470 *dv = visible ? vf - vi : 0.f;
2472 for (int i = -1; i < 3; i++) {
2473 for (int j = -1; j < 3; j++) {
2474 us[i + 1][j + 1] = visible ? av_clip(ui + j, 0, width - 1) : 0;
2475 vs[i + 1][j + 1] = visible ? av_clip(vi + i, 0, height - 1) : 0;
2483 * Calculate 3D coordinates on sphere for corresponding frame position in pannini format.
2485 * @param s filter private context
2486 * @param i horizontal position on frame [0, width)
2487 * @param j vertical position on frame [0, height)
2488 * @param width frame width
2489 * @param height frame height
2490 * @param vec coordinates on sphere
2492 static int pannini_to_xyz(const V360Context *s,
2493 int i, int j, int width, int height,
2496 const float uf = ((2.f * i) / width - 1.f);
2497 const float vf = ((2.f * j) / height - 1.f);
2499 const float d = s->h_fov;
2500 const float k = uf * uf / ((d + 1.f) * (d + 1.f));
2501 const float dscr = k * k * d * d - (k + 1.f) * (k * d * d - 1.f);
2502 const float clon = (-k * d + sqrtf(dscr)) / (k + 1.f);
2503 const float S = (d + 1.f) / (d + clon);
2504 const float lon = -(M_PI + atan2f(uf, S * clon));
2505 const float lat = -atan2f(vf, S);
2507 vec[0] = sinf(lon) * cosf(lat);
2509 vec[2] = cosf(lon) * cosf(lat);
2511 normalize_vector(vec);
2517 * Prepare data for processing cylindrical output format.
2519 * @param ctx filter context
2521 * @return error code
2523 static int prepare_cylindrical_out(AVFilterContext *ctx)
2525 V360Context *s = ctx->priv;
2527 s->flat_range[0] = M_PI * s->h_fov / 360.f;
2528 s->flat_range[1] = tanf(0.5f * s->v_fov * M_PI / 180.f);
2534 * Calculate 3D coordinates on sphere for corresponding frame position in cylindrical format.
2536 * @param s filter private context
2537 * @param i horizontal position on frame [0, width)
2538 * @param j vertical position on frame [0, height)
2539 * @param width frame width
2540 * @param height frame height
2541 * @param vec coordinates on sphere
2543 static int cylindrical_to_xyz(const V360Context *s,
2544 int i, int j, int width, int height,
2547 const float uf = s->flat_range[0] * ((2.f * i) / width - 1.f);
2548 const float vf = s->flat_range[1] * ((2.f * j) / height - 1.f);
2550 const float phi = uf;
2551 const float theta = atanf(vf);
2553 const float sin_phi = sinf(phi);
2554 const float cos_phi = cosf(phi);
2555 const float sin_theta = sinf(theta);
2556 const float cos_theta = cosf(theta);
2558 vec[0] = cos_theta * sin_phi;
2559 vec[1] = -sin_theta;
2560 vec[2] = -cos_theta * cos_phi;
2562 normalize_vector(vec);
2568 * Prepare data for processing cylindrical input format.
2570 * @param ctx filter context
2572 * @return error code
2574 static int prepare_cylindrical_in(AVFilterContext *ctx)
2576 V360Context *s = ctx->priv;
2578 s->iflat_range[0] = M_PI * s->ih_fov / 360.f;
2579 s->iflat_range[1] = tanf(0.5f * s->iv_fov * M_PI / 180.f);
2585 * Calculate frame position in cylindrical format for corresponding 3D coordinates on sphere.
2587 * @param s filter private context
2588 * @param vec coordinates on sphere
2589 * @param width frame width
2590 * @param height frame height
2591 * @param us horizontal coordinates for interpolation window
2592 * @param vs vertical coordinates for interpolation window
2593 * @param du horizontal relative coordinate
2594 * @param dv vertical relative coordinate
2596 static int xyz_to_cylindrical(const V360Context *s,
2597 const float *vec, int width, int height,
2598 int16_t us[4][4], int16_t vs[4][4], float *du, float *dv)
2600 const float phi = atan2f(vec[0], -vec[2]) * s->input_mirror_modifier[0] / s->iflat_range[0];
2601 const float theta = atan2f(-vec[1], hypotf(vec[0], vec[2])) * s->input_mirror_modifier[1] / s->iflat_range[1];
2602 int visible, ui, vi;
2605 uf = (phi + 1.f) * (width - 1) / 2.f;
2606 vf = (tanf(theta) + 1.f) * height / 2.f;
2610 visible = vi >= 0 && vi < height && ui >= 0 && ui < width &&
2611 theta <= M_PI * s->iv_fov / 180.f &&
2612 theta >= -M_PI * s->iv_fov / 180.f;
2617 for (int i = -1; i < 3; i++) {
2618 for (int j = -1; j < 3; j++) {
2619 us[i + 1][j + 1] = visible ? av_clip(ui + j, 0, width - 1) : 0;
2620 vs[i + 1][j + 1] = visible ? av_clip(vi + i, 0, height - 1) : 0;
2628 * Calculate 3D coordinates on sphere for corresponding frame position in perspective format.
2630 * @param s filter private context
2631 * @param i horizontal position on frame [0, width)
2632 * @param j vertical position on frame [0, height)
2633 * @param width frame width
2634 * @param height frame height
2635 * @param vec coordinates on sphere
2637 static int perspective_to_xyz(const V360Context *s,
2638 int i, int j, int width, int height,
2641 const float uf = ((2.f * i) / width - 1.f);
2642 const float vf = ((2.f * j) / height - 1.f);
2643 const float rh = hypotf(uf, vf);
2644 const float sinzz = 1.f - rh * rh;
2645 const float h = 1.f + s->v_fov;
2646 const float sinz = (h - sqrtf(sinzz)) / (h / rh + rh / h);
2647 const float sinz2 = sinz * sinz;
2650 const float cosz = sqrtf(1.f - sinz2);
2652 const float theta = asinf(cosz);
2653 const float phi = atan2f(uf, vf);
2655 const float sin_phi = sinf(phi);
2656 const float cos_phi = cosf(phi);
2657 const float sin_theta = sinf(theta);
2658 const float cos_theta = cosf(theta);
2660 vec[0] = cos_theta * sin_phi;
2662 vec[2] = -cos_theta * cos_phi;
2670 normalize_vector(vec);
2675 * Calculate 3D coordinates on sphere for corresponding frame position in tetrahedron format.
2677 * @param s filter private context
2678 * @param i horizontal position on frame [0, width)
2679 * @param j vertical position on frame [0, height)
2680 * @param width frame width
2681 * @param height frame height
2682 * @param vec coordinates on sphere
2684 static int tetrahedron_to_xyz(const V360Context *s,
2685 int i, int j, int width, int height,
2688 const float uf = (float)i / width;
2689 const float vf = (float)j / height;
2691 vec[0] = uf < 0.5f ? uf * 4.f - 1.f : 3.f - uf * 4.f;
2692 vec[1] = 1.f - vf * 2.f;
2693 vec[2] = 2.f * fabsf(1.f - fabsf(1.f - uf * 2.f + vf)) - 1.f;
2695 normalize_vector(vec);
2701 * Calculate frame position in tetrahedron format for corresponding 3D coordinates on sphere.
2703 * @param s filter private context
2704 * @param vec coordinates on sphere
2705 * @param width frame width
2706 * @param height frame height
2707 * @param us horizontal coordinates for interpolation window
2708 * @param vs vertical coordinates for interpolation window
2709 * @param du horizontal relative coordinate
2710 * @param dv vertical relative coordinate
2712 static int xyz_to_tetrahedron(const V360Context *s,
2713 const float *vec, int width, int height,
2714 int16_t us[4][4], int16_t vs[4][4], float *du, float *dv)
2716 float d = 0.5f * (vec[0] * vec[0] + vec[1] * vec[1] + vec[2] * vec[2]);
2718 const float d0 = (vec[0] * 0.5f + vec[1] * 0.5f + vec[2] *-0.5f) / d;
2719 const float d1 = (vec[0] *-0.5f + vec[1] *-0.5f + vec[2] *-0.5f) / d;
2720 const float d2 = (vec[0] * 0.5f + vec[1] *-0.5f + vec[2] * 0.5f) / d;
2721 const float d3 = (vec[0] *-0.5f + vec[1] * 0.5f + vec[2] * 0.5f) / d;
2723 float uf, vf, x, y, z;
2726 d = FFMAX(d0, FFMAX3(d1, d2, d3));
2732 vf = 0.5f - y * 0.5f * s->input_mirror_modifier[1];
2734 if ((x + y >= 0.f && y + z >= 0.f && -z - x <= 0.f) ||
2735 (x + y <= 0.f && -y + z >= 0.f && z - x >= 0.f)) {
2736 uf = 0.25f * x * s->input_mirror_modifier[0] + 0.25f;
2738 uf = 0.75f - 0.25f * x * s->input_mirror_modifier[0];
2750 for (int i = -1; i < 3; i++) {
2751 for (int j = -1; j < 3; j++) {
2752 us[i + 1][j + 1] = mod(ui + j, width);
2753 vs[i + 1][j + 1] = av_clip(vi + i, 0, height - 1);
2761 * Calculate 3D coordinates on sphere for corresponding frame position in dual fisheye format.
2763 * @param s filter private context
2764 * @param i horizontal position on frame [0, width)
2765 * @param j vertical position on frame [0, height)
2766 * @param width frame width
2767 * @param height frame height
2768 * @param vec coordinates on sphere
2770 static int dfisheye_to_xyz(const V360Context *s,
2771 int i, int j, int width, int height,
2774 const float scale = 1.f + s->out_pad;
2776 const float ew = width / 2.f;
2777 const float eh = height;
2779 const int ei = i >= ew ? i - ew : i;
2780 const float m = i >= ew ? -1.f : 1.f;
2782 const float uf = ((2.f * ei) / ew - 1.f) * scale;
2783 const float vf = ((2.f * j + 1.f) / eh - 1.f) * scale;
2785 const float h = hypotf(uf, vf);
2786 const float lh = h > 0.f ? h : 1.f;
2787 const float theta = m * M_PI_2 * (1.f - h);
2789 const float sin_theta = sinf(theta);
2790 const float cos_theta = cosf(theta);
2792 vec[0] = cos_theta * m * -uf / lh;
2793 vec[1] = cos_theta * -vf / lh;
2796 normalize_vector(vec);
2802 * Calculate frame position in dual fisheye format for corresponding 3D coordinates on sphere.
2804 * @param s filter private context
2805 * @param vec coordinates on sphere
2806 * @param width frame width
2807 * @param height frame height
2808 * @param us horizontal coordinates for interpolation window
2809 * @param vs vertical coordinates for interpolation window
2810 * @param du horizontal relative coordinate
2811 * @param dv vertical relative coordinate
2813 static int xyz_to_dfisheye(const V360Context *s,
2814 const float *vec, int width, int height,
2815 int16_t us[4][4], int16_t vs[4][4], float *du, float *dv)
2817 const float scale = 1.f - s->in_pad;
2819 const float ew = width / 2.f;
2820 const float eh = height;
2822 const float h = hypotf(vec[0], vec[1]);
2823 const float lh = h > 0.f ? h : 1.f;
2824 const float theta = acosf(fabsf(vec[2])) / M_PI;
2826 float uf = (theta * (-vec[0] / lh) * s->input_mirror_modifier[0] * scale + 0.5f) * ew;
2827 float vf = (theta * (-vec[1] / lh) * s->input_mirror_modifier[1] * scale + 0.5f) * eh;
2832 if (vec[2] >= 0.f) {
2835 u_shift = ceilf(ew);
2845 for (int i = -1; i < 3; i++) {
2846 for (int j = -1; j < 3; j++) {
2847 us[i + 1][j + 1] = av_clip(u_shift + ui + j, 0, width - 1);
2848 vs[i + 1][j + 1] = av_clip( vi + i, 0, height - 1);
2856 * Calculate 3D coordinates on sphere for corresponding frame position in barrel facebook's format.
2858 * @param s filter private context
2859 * @param i horizontal position on frame [0, width)
2860 * @param j vertical position on frame [0, height)
2861 * @param width frame width
2862 * @param height frame height
2863 * @param vec coordinates on sphere
2865 static int barrel_to_xyz(const V360Context *s,
2866 int i, int j, int width, int height,
2869 const float scale = 0.99f;
2870 float l_x, l_y, l_z;
2872 if (i < 4 * width / 5) {
2873 const float theta_range = M_PI_4;
2875 const int ew = 4 * width / 5;
2876 const int eh = height;
2878 const float phi = ((2.f * i) / ew - 1.f) * M_PI / scale;
2879 const float theta = ((2.f * j) / eh - 1.f) * theta_range / scale;
2881 const float sin_phi = sinf(phi);
2882 const float cos_phi = cosf(phi);
2883 const float sin_theta = sinf(theta);
2884 const float cos_theta = cosf(theta);
2886 l_x = cos_theta * sin_phi;
2888 l_z = -cos_theta * cos_phi;
2890 const int ew = width / 5;
2891 const int eh = height / 2;
2896 uf = 2.f * (i - 4 * ew) / ew - 1.f;
2897 vf = 2.f * (j ) / eh - 1.f;
2906 uf = 2.f * (i - 4 * ew) / ew - 1.f;
2907 vf = 2.f * (j - eh) / eh - 1.f;
2922 normalize_vector(vec);
2928 * Calculate frame position in barrel facebook's format for corresponding 3D coordinates on sphere.
2930 * @param s filter private context
2931 * @param vec coordinates on sphere
2932 * @param width frame width
2933 * @param height frame height
2934 * @param us horizontal coordinates for interpolation window
2935 * @param vs vertical coordinates for interpolation window
2936 * @param du horizontal relative coordinate
2937 * @param dv vertical relative coordinate
2939 static int xyz_to_barrel(const V360Context *s,
2940 const float *vec, int width, int height,
2941 int16_t us[4][4], int16_t vs[4][4], float *du, float *dv)
2943 const float scale = 0.99f;
2945 const float phi = atan2f(vec[0], -vec[2]) * s->input_mirror_modifier[0];
2946 const float theta = asinf(-vec[1]) * s->input_mirror_modifier[1];
2947 const float theta_range = M_PI_4;
2950 int u_shift, v_shift;
2954 if (theta > -theta_range && theta < theta_range) {
2958 u_shift = s->ih_flip ? width / 5 : 0;
2961 uf = (phi / M_PI * scale + 1.f) * ew / 2.f;
2962 vf = (theta / theta_range * scale + 1.f) * eh / 2.f;
2967 u_shift = s->ih_flip ? 0 : 4 * ew;
2969 if (theta < 0.f) { // UP
2970 uf = vec[0] / vec[1];
2971 vf = -vec[2] / vec[1];
2974 uf = -vec[0] / vec[1];
2975 vf = -vec[2] / vec[1];
2979 uf *= s->input_mirror_modifier[0] * s->input_mirror_modifier[1];
2980 vf *= s->input_mirror_modifier[1];
2982 uf = 0.5f * ew * (uf * scale + 1.f);
2983 vf = 0.5f * eh * (vf * scale + 1.f);
2992 for (int i = -1; i < 3; i++) {
2993 for (int j = -1; j < 3; j++) {
2994 us[i + 1][j + 1] = u_shift + av_clip(ui + j, 0, ew - 1);
2995 vs[i + 1][j + 1] = v_shift + av_clip(vi + i, 0, eh - 1);
3003 * Calculate frame position in barrel split facebook's format for corresponding 3D coordinates on sphere.
3005 * @param s filter private context
3006 * @param vec coordinates on sphere
3007 * @param width frame width
3008 * @param height frame height
3009 * @param us horizontal coordinates for interpolation window
3010 * @param vs vertical coordinates for interpolation window
3011 * @param du horizontal relative coordinate
3012 * @param dv vertical relative coordinate
3014 static int xyz_to_barrelsplit(const V360Context *s,
3015 const float *vec, int width, int height,
3016 int16_t us[4][4], int16_t vs[4][4], float *du, float *dv)
3018 const float phi = atan2f(vec[0], -vec[2]) * s->input_mirror_modifier[0];
3019 const float theta = asinf(-vec[1]) * s->input_mirror_modifier[1];
3021 const float theta_range = M_PI_4;
3024 int u_shift, v_shift;
3028 if (theta >= -theta_range && theta <= theta_range) {
3029 const float scalew = s->fin_pad > 0 ? 1.f - s->fin_pad / (width * 2.f / 3.f) : 1.f - s->in_pad;
3030 const float scaleh = s->fin_pad > 0 ? 1.f - s->fin_pad / (height / 2.f) : 1.f - s->in_pad;
3035 u_shift = s->ih_flip ? width / 3 : 0;
3036 v_shift = phi >= M_PI_2 || phi < -M_PI_2 ? eh : 0;
3038 uf = fmodf(phi, M_PI_2) / M_PI_2;
3039 vf = theta / M_PI_4;
3042 uf = uf >= 0.f ? fmodf(uf - 1.f, 1.f) : fmodf(uf + 1.f, 1.f);
3044 uf = (uf * scalew + 1.f) * width / 3.f;
3045 vf = (vf * scaleh + 1.f) * height / 4.f;
3047 const float scalew = s->fin_pad > 0 ? 1.f - s->fin_pad / (width / 3.f) : 1.f - s->in_pad;
3048 const float scaleh = s->fin_pad > 0 ? 1.f - s->fin_pad / (height / 4.f) : 1.f - s->in_pad;
3054 u_shift = s->ih_flip ? 0 : 2 * ew;
3056 if (theta <= 0.f && theta >= -M_PI_2 &&
3057 phi <= M_PI_2 && phi >= -M_PI_2) {
3058 uf = vec[0] / vec[1];
3059 vf = -vec[2] / vec[1];
3062 } else if (theta >= 0.f && theta <= M_PI_2 &&
3063 phi <= M_PI_2 && phi >= -M_PI_2) {
3064 uf = -vec[0] / vec[1];
3065 vf = -vec[2] / vec[1];
3066 v_shift = height * 0.25f;
3067 } else if (theta <= 0.f && theta >= -M_PI_2) {
3068 uf = -vec[0] / vec[1];
3069 vf = vec[2] / vec[1];
3070 v_shift = height * 0.5f;
3073 uf = vec[0] / vec[1];
3074 vf = vec[2] / vec[1];
3075 v_shift = height * 0.75f;
3078 uf *= s->input_mirror_modifier[0] * s->input_mirror_modifier[1];
3079 vf *= s->input_mirror_modifier[1];
3081 uf = 0.5f * width / 3.f * (uf * scalew + 1.f);
3082 vf = height * 0.25f * (vf * scaleh + 1.f) + v_offset;
3091 for (int i = -1; i < 3; i++) {
3092 for (int j = -1; j < 3; j++) {
3093 us[i + 1][j + 1] = u_shift + av_clip(ui + j, 0, ew - 1);
3094 vs[i + 1][j + 1] = v_shift + av_clip(vi + i, 0, eh - 1);
3102 * Calculate 3D coordinates on sphere for corresponding frame position in barrel split facebook's format.
3104 * @param s filter private context
3105 * @param i horizontal position on frame [0, width)
3106 * @param j vertical position on frame [0, height)
3107 * @param width frame width
3108 * @param height frame height
3109 * @param vec coordinates on sphere
3111 static int barrelsplit_to_xyz(const V360Context *s,
3112 int i, int j, int width, int height,
3115 const float x = (i + 0.5f) / width;
3116 const float y = (j + 0.5f) / height;
3117 float l_x, l_y, l_z;
3119 if (x < 2.f / 3.f) {
3120 const float scalew = s->fout_pad > 0 ? 1.f - s->fout_pad / (width * 2.f / 3.f) : 1.f - s->out_pad;
3121 const float scaleh = s->fout_pad > 0 ? 1.f - s->fout_pad / (height / 2.f) : 1.f - s->out_pad;
3123 const float back = floorf(y * 2.f);
3125 const float phi = ((3.f / 2.f * x - 0.5f) / scalew - back + 1.f) * M_PI;
3126 const float theta = (y - 0.25f - 0.5f * back) / scaleh * M_PI;
3128 const float sin_phi = sinf(phi);
3129 const float cos_phi = cosf(phi);
3130 const float sin_theta = sinf(theta);
3131 const float cos_theta = cosf(theta);
3133 l_x = -cos_theta * sin_phi;
3135 l_z = cos_theta * cos_phi;
3137 const float scalew = s->fout_pad > 0 ? 1.f - s->fout_pad / (width / 3.f) : 1.f - s->out_pad;
3138 const float scaleh = s->fout_pad > 0 ? 1.f - s->fout_pad / (height / 4.f) : 1.f - s->out_pad;
3140 const int face = floorf(y * 4.f);
3151 l_x = (0.5f - uf) / scalew;
3153 l_z = (-0.5f + vf) / scaleh;
3158 vf = 1.f - (vf - 0.5f);
3160 l_x = (0.5f - uf) / scalew;
3162 l_z = (0.5f - vf) / scaleh;
3165 vf = y * 2.f - 0.5f;
3166 vf = 1.f - (1.f - vf);
3168 l_x = (0.5f - uf) / scalew;
3170 l_z = (-0.5f + vf) / scaleh;
3173 vf = y * 2.f - 1.5f;
3175 l_x = (0.5f - uf) / scalew;
3177 l_z = (0.5f - vf) / scaleh;
3186 normalize_vector(vec);
3191 static void multiply_matrix(float c[3][3], const float a[3][3], const float b[3][3])
3193 for (int i = 0; i < 3; i++) {
3194 for (int j = 0; j < 3; j++) {
3197 for (int k = 0; k < 3; k++)
3198 sum += a[i][k] * b[k][j];
3206 * Calculate rotation matrix for yaw/pitch/roll angles.
3208 static inline void calculate_rotation_matrix(float yaw, float pitch, float roll,
3209 float rot_mat[3][3],
3210 const int rotation_order[3])
3212 const float yaw_rad = yaw * M_PI / 180.f;
3213 const float pitch_rad = pitch * M_PI / 180.f;
3214 const float roll_rad = roll * M_PI / 180.f;
3216 const float sin_yaw = sinf(-yaw_rad);
3217 const float cos_yaw = cosf(-yaw_rad);
3218 const float sin_pitch = sinf(pitch_rad);
3219 const float cos_pitch = cosf(pitch_rad);
3220 const float sin_roll = sinf(roll_rad);
3221 const float cos_roll = cosf(roll_rad);
3226 m[0][0][0] = cos_yaw; m[0][0][1] = 0; m[0][0][2] = sin_yaw;
3227 m[0][1][0] = 0; m[0][1][1] = 1; m[0][1][2] = 0;
3228 m[0][2][0] = -sin_yaw; m[0][2][1] = 0; m[0][2][2] = cos_yaw;
3230 m[1][0][0] = 1; m[1][0][1] = 0; m[1][0][2] = 0;
3231 m[1][1][0] = 0; m[1][1][1] = cos_pitch; m[1][1][2] = -sin_pitch;
3232 m[1][2][0] = 0; m[1][2][1] = sin_pitch; m[1][2][2] = cos_pitch;
3234 m[2][0][0] = cos_roll; m[2][0][1] = -sin_roll; m[2][0][2] = 0;
3235 m[2][1][0] = sin_roll; m[2][1][1] = cos_roll; m[2][1][2] = 0;
3236 m[2][2][0] = 0; m[2][2][1] = 0; m[2][2][2] = 1;
3238 multiply_matrix(temp, m[rotation_order[0]], m[rotation_order[1]]);
3239 multiply_matrix(rot_mat, temp, m[rotation_order[2]]);
3243 * Rotate vector with given rotation matrix.
3245 * @param rot_mat rotation matrix
3248 static inline void rotate(const float rot_mat[3][3],
3251 const float x_tmp = vec[0] * rot_mat[0][0] + vec[1] * rot_mat[0][1] + vec[2] * rot_mat[0][2];
3252 const float y_tmp = vec[0] * rot_mat[1][0] + vec[1] * rot_mat[1][1] + vec[2] * rot_mat[1][2];
3253 const float z_tmp = vec[0] * rot_mat[2][0] + vec[1] * rot_mat[2][1] + vec[2] * rot_mat[2][2];
3260 static inline void set_mirror_modifier(int h_flip, int v_flip, int d_flip,
3263 modifier[0] = h_flip ? -1.f : 1.f;
3264 modifier[1] = v_flip ? -1.f : 1.f;
3265 modifier[2] = d_flip ? -1.f : 1.f;
3268 static inline void mirror(const float *modifier, float *vec)
3270 vec[0] *= modifier[0];
3271 vec[1] *= modifier[1];
3272 vec[2] *= modifier[2];
3275 static int allocate_plane(V360Context *s, int sizeof_uv, int sizeof_ker, int sizeof_mask, int p)
3278 s->u[p] = av_calloc(s->uv_linesize[p] * s->pr_height[p], sizeof_uv);
3280 s->v[p] = av_calloc(s->uv_linesize[p] * s->pr_height[p], sizeof_uv);
3281 if (!s->u[p] || !s->v[p])
3282 return AVERROR(ENOMEM);
3285 s->ker[p] = av_calloc(s->uv_linesize[p] * s->pr_height[p], sizeof_ker);
3287 return AVERROR(ENOMEM);
3290 if (sizeof_mask && !p) {
3292 s->mask = av_calloc(s->pr_width[p] * s->pr_height[p], sizeof_mask);
3294 return AVERROR(ENOMEM);
3300 static void fov_from_dfov(int format, float d_fov, float w, float h, float *h_fov, float *v_fov)
3305 const float d = 0.5f * hypotf(w, h);
3307 *h_fov = d / h * d_fov;
3308 *v_fov = d / w * d_fov;
3314 const float da = tanf(0.5 * FFMIN(d_fov, 359.f) * M_PI / 180.f);
3315 const float d = hypotf(w, h);
3317 *h_fov = atan2f(da * w, d) * 360.f / M_PI;
3318 *v_fov = atan2f(da * h, d) * 360.f / M_PI;
3329 static void set_dimensions(int *outw, int *outh, int w, int h, const AVPixFmtDescriptor *desc)
3331 outw[1] = outw[2] = FF_CEIL_RSHIFT(w, desc->log2_chroma_w);
3332 outw[0] = outw[3] = w;
3333 outh[1] = outh[2] = FF_CEIL_RSHIFT(h, desc->log2_chroma_h);
3334 outh[0] = outh[3] = h;
3337 // Calculate remap data
3338 static av_always_inline int v360_slice(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
3340 V360Context *s = ctx->priv;
3342 for (int p = 0; p < s->nb_allocated; p++) {
3343 const int max_value = s->max_value;
3344 const int width = s->pr_width[p];
3345 const int uv_linesize = s->uv_linesize[p];
3346 const int height = s->pr_height[p];
3347 const int in_width = s->inplanewidth[p];
3348 const int in_height = s->inplaneheight[p];
3349 const int slice_start = (height * jobnr ) / nb_jobs;
3350 const int slice_end = (height * (jobnr + 1)) / nb_jobs;
3355 for (int j = slice_start; j < slice_end; j++) {
3356 for (int i = 0; i < width; i++) {
3357 int16_t *u = s->u[p] + (j * uv_linesize + i) * s->elements;
3358 int16_t *v = s->v[p] + (j * uv_linesize + i) * s->elements;
3359 int16_t *ker = s->ker[p] + (j * uv_linesize + i) * s->elements;
3360 uint8_t *mask8 = p ? NULL : s->mask + (j * s->pr_width[0] + i);
3361 uint16_t *mask16 = p ? NULL : (uint16_t *)s->mask + (j * s->pr_width[0] + i);
3362 int in_mask, out_mask;
3364 if (s->out_transpose)
3365 out_mask = s->out_transform(s, j, i, height, width, vec);
3367 out_mask = s->out_transform(s, i, j, width, height, vec);
3368 av_assert1(!isnan(vec[0]) && !isnan(vec[1]) && !isnan(vec[2]));
3369 rotate(s->rot_mat, vec);
3370 av_assert1(!isnan(vec[0]) && !isnan(vec[1]) && !isnan(vec[2]));
3371 normalize_vector(vec);
3372 mirror(s->output_mirror_modifier, vec);
3373 if (s->in_transpose)
3374 in_mask = s->in_transform(s, vec, in_height, in_width, rmap.v, rmap.u, &du, &dv);
3376 in_mask = s->in_transform(s, vec, in_width, in_height, rmap.u, rmap.v, &du, &dv);
3377 av_assert1(!isnan(du) && !isnan(dv));
3378 s->calculate_kernel(du, dv, &rmap, u, v, ker);
3380 if (!p && s->mask) {
3381 if (s->mask_size == 1) {
3382 mask8[0] = 255 * (out_mask & in_mask);
3384 mask16[0] = max_value * (out_mask & in_mask);
3394 static int config_output(AVFilterLink *outlink)
3396 AVFilterContext *ctx = outlink->src;
3397 AVFilterLink *inlink = ctx->inputs[0];
3398 V360Context *s = ctx->priv;
3399 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(inlink->format);
3400 const int depth = desc->comp[0].depth;
3401 const int sizeof_mask = s->mask_size = (depth + 7) >> 3;
3406 int in_offset_h, in_offset_w;
3407 int out_offset_h, out_offset_w;
3409 int (*prepare_out)(AVFilterContext *ctx);
3412 s->max_value = (1 << depth) - 1;
3413 s->input_mirror_modifier[0] = s->ih_flip ? -1.f : 1.f;
3414 s->input_mirror_modifier[1] = s->iv_flip ? -1.f : 1.f;
3416 switch (s->interp) {
3418 s->calculate_kernel = nearest_kernel;
3419 s->remap_slice = depth <= 8 ? remap1_8bit_slice : remap1_16bit_slice;
3421 sizeof_uv = sizeof(int16_t) * s->elements;
3425 s->calculate_kernel = bilinear_kernel;
3426 s->remap_slice = depth <= 8 ? remap2_8bit_slice : remap2_16bit_slice;
3427 s->elements = 2 * 2;
3428 sizeof_uv = sizeof(int16_t) * s->elements;
3429 sizeof_ker = sizeof(int16_t) * s->elements;
3432 s->calculate_kernel = bicubic_kernel;
3433 s->remap_slice = depth <= 8 ? remap4_8bit_slice : remap4_16bit_slice;
3434 s->elements = 4 * 4;
3435 sizeof_uv = sizeof(int16_t) * s->elements;
3436 sizeof_ker = sizeof(int16_t) * s->elements;
3439 s->calculate_kernel = lanczos_kernel;
3440 s->remap_slice = depth <= 8 ? remap4_8bit_slice : remap4_16bit_slice;
3441 s->elements = 4 * 4;
3442 sizeof_uv = sizeof(int16_t) * s->elements;
3443 sizeof_ker = sizeof(int16_t) * s->elements;
3446 s->calculate_kernel = spline16_kernel;
3447 s->remap_slice = depth <= 8 ? remap4_8bit_slice : remap4_16bit_slice;
3448 s->elements = 4 * 4;
3449 sizeof_uv = sizeof(int16_t) * s->elements;
3450 sizeof_ker = sizeof(int16_t) * s->elements;
3453 s->calculate_kernel = gaussian_kernel;
3454 s->remap_slice = depth <= 8 ? remap4_8bit_slice : remap4_16bit_slice;
3455 s->elements = 4 * 4;
3456 sizeof_uv = sizeof(int16_t) * s->elements;
3457 sizeof_ker = sizeof(int16_t) * s->elements;
3463 ff_v360_init(s, depth);
3465 for (int order = 0; order < NB_RORDERS; order++) {
3466 const char c = s->rorder[order];
3470 av_log(ctx, AV_LOG_WARNING,
3471 "Incomplete rorder option. Direction for all 3 rotation orders should be specified. Switching to default rorder.\n");
3472 s->rotation_order[0] = YAW;
3473 s->rotation_order[1] = PITCH;
3474 s->rotation_order[2] = ROLL;
3478 rorder = get_rorder(c);
3480 av_log(ctx, AV_LOG_WARNING,
3481 "Incorrect rotation order symbol '%c' in rorder option. Switching to default rorder.\n", c);
3482 s->rotation_order[0] = YAW;
3483 s->rotation_order[1] = PITCH;
3484 s->rotation_order[2] = ROLL;
3488 s->rotation_order[order] = rorder;
3491 switch (s->in_stereo) {
3495 in_offset_w = in_offset_h = 0;
3513 set_dimensions(s->inplanewidth, s->inplaneheight, w, h, desc);
3514 set_dimensions(s->in_offset_w, s->in_offset_h, in_offset_w, in_offset_h, desc);
3516 s->in_width = s->inplanewidth[0];
3517 s->in_height = s->inplaneheight[0];
3519 if (s->id_fov > 0.f)
3520 fov_from_dfov(s->in, s->id_fov, w, h, &s->ih_fov, &s->iv_fov);
3522 if (s->in_transpose)
3523 FFSWAP(int, s->in_width, s->in_height);
3526 case EQUIRECTANGULAR:
3527 s->in_transform = xyz_to_equirect;
3533 s->in_transform = xyz_to_cube3x2;
3534 err = prepare_cube_in(ctx);
3539 s->in_transform = xyz_to_cube1x6;
3540 err = prepare_cube_in(ctx);
3545 s->in_transform = xyz_to_cube6x1;
3546 err = prepare_cube_in(ctx);
3551 s->in_transform = xyz_to_eac;
3552 err = prepare_eac_in(ctx);
3557 s->in_transform = xyz_to_flat;
3558 err = prepare_flat_in(ctx);
3564 av_log(ctx, AV_LOG_ERROR, "Supplied format is not accepted as input.\n");
3565 return AVERROR(EINVAL);
3567 s->in_transform = xyz_to_dfisheye;
3573 s->in_transform = xyz_to_barrel;
3579 s->in_transform = xyz_to_stereographic;
3580 err = prepare_stereographic_in(ctx);
3585 s->in_transform = xyz_to_mercator;
3591 s->in_transform = xyz_to_ball;
3597 s->in_transform = xyz_to_hammer;
3603 s->in_transform = xyz_to_sinusoidal;
3609 s->in_transform = xyz_to_fisheye;
3610 err = prepare_fisheye_in(ctx);
3615 s->in_transform = xyz_to_cylindrical;
3616 err = prepare_cylindrical_in(ctx);
3621 s->in_transform = xyz_to_tetrahedron;
3627 s->in_transform = xyz_to_barrelsplit;
3633 av_log(ctx, AV_LOG_ERROR, "Specified input format is not handled.\n");
3642 case EQUIRECTANGULAR:
3643 s->out_transform = equirect_to_xyz;
3649 s->out_transform = cube3x2_to_xyz;
3650 prepare_out = prepare_cube_out;
3651 w = lrintf(wf / 4.f * 3.f);
3655 s->out_transform = cube1x6_to_xyz;
3656 prepare_out = prepare_cube_out;
3657 w = lrintf(wf / 4.f);
3658 h = lrintf(hf * 3.f);
3661 s->out_transform = cube6x1_to_xyz;
3662 prepare_out = prepare_cube_out;
3663 w = lrintf(wf / 2.f * 3.f);
3664 h = lrintf(hf / 2.f);
3667 s->out_transform = eac_to_xyz;
3668 prepare_out = prepare_eac_out;
3670 h = lrintf(hf / 8.f * 9.f);
3673 s->out_transform = flat_to_xyz;
3674 prepare_out = prepare_flat_out;
3679 s->out_transform = dfisheye_to_xyz;
3685 s->out_transform = barrel_to_xyz;
3687 w = lrintf(wf / 4.f * 5.f);
3691 s->out_transform = stereographic_to_xyz;
3692 prepare_out = prepare_stereographic_out;
3694 h = lrintf(hf * 2.f);
3697 s->out_transform = mercator_to_xyz;
3700 h = lrintf(hf * 2.f);
3703 s->out_transform = ball_to_xyz;
3706 h = lrintf(hf * 2.f);
3709 s->out_transform = hammer_to_xyz;
3715 s->out_transform = sinusoidal_to_xyz;
3721 s->out_transform = fisheye_to_xyz;
3722 prepare_out = prepare_fisheye_out;
3723 w = lrintf(wf * 0.5f);
3727 s->out_transform = pannini_to_xyz;
3733 s->out_transform = cylindrical_to_xyz;
3734 prepare_out = prepare_cylindrical_out;
3736 h = lrintf(hf * 0.5f);
3739 s->out_transform = perspective_to_xyz;
3741 w = lrintf(wf / 2.f);
3745 s->out_transform = tetrahedron_to_xyz;
3751 s->out_transform = barrelsplit_to_xyz;
3753 w = lrintf(wf / 4.f * 3.f);
3757 av_log(ctx, AV_LOG_ERROR, "Specified output format is not handled.\n");
3761 // Override resolution with user values if specified
3762 if (s->width > 0 && s->height > 0) {
3765 } else if (s->width > 0 || s->height > 0) {
3766 av_log(ctx, AV_LOG_ERROR, "Both width and height values should be specified.\n");
3767 return AVERROR(EINVAL);
3769 if (s->out_transpose)
3772 if (s->in_transpose)
3780 fov_from_dfov(s->out, s->d_fov, w, h, &s->h_fov, &s->v_fov);
3783 err = prepare_out(ctx);
3788 set_dimensions(s->pr_width, s->pr_height, w, h, desc);
3790 s->out_width = s->pr_width[0];
3791 s->out_height = s->pr_height[0];
3793 if (s->out_transpose)
3794 FFSWAP(int, s->out_width, s->out_height);
3796 switch (s->out_stereo) {
3798 out_offset_w = out_offset_h = 0;
3814 set_dimensions(s->out_offset_w, s->out_offset_h, out_offset_w, out_offset_h, desc);
3815 set_dimensions(s->planewidth, s->planeheight, w, h, desc);
3817 for (int i = 0; i < 4; i++)
3818 s->uv_linesize[i] = FFALIGN(s->pr_width[i], 8);
3823 s->nb_planes = av_pix_fmt_count_planes(inlink->format);
3824 have_alpha = !!(desc->flags & AV_PIX_FMT_FLAG_ALPHA);
3826 if (desc->log2_chroma_h == desc->log2_chroma_w && desc->log2_chroma_h == 0) {
3827 s->nb_allocated = 1;
3828 s->map[0] = s->map[1] = s->map[2] = s->map[3] = 0;
3830 s->nb_allocated = 2;
3831 s->map[0] = s->map[3] = 0;
3832 s->map[1] = s->map[2] = 1;
3835 for (int i = 0; i < s->nb_allocated; i++)
3836 allocate_plane(s, sizeof_uv, sizeof_ker, sizeof_mask * have_alpha * s->alpha, i);
3838 calculate_rotation_matrix(s->yaw, s->pitch, s->roll, s->rot_mat, s->rotation_order);
3839 set_mirror_modifier(s->h_flip, s->v_flip, s->d_flip, s->output_mirror_modifier);
3841 ctx->internal->execute(ctx, v360_slice, NULL, NULL, FFMIN(outlink->h, ff_filter_get_nb_threads(ctx)));
3846 static int filter_frame(AVFilterLink *inlink, AVFrame *in)
3848 AVFilterContext *ctx = inlink->dst;
3849 AVFilterLink *outlink = ctx->outputs[0];
3850 V360Context *s = ctx->priv;
3854 out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
3857 return AVERROR(ENOMEM);
3859 av_frame_copy_props(out, in);
3864 ctx->internal->execute(ctx, s->remap_slice, &td, NULL, FFMIN(outlink->h, ff_filter_get_nb_threads(ctx)));
3867 return ff_filter_frame(outlink, out);
3870 static int process_command(AVFilterContext *ctx, const char *cmd, const char *args,
3871 char *res, int res_len, int flags)
3875 ret = ff_filter_process_command(ctx, cmd, args, res, res_len, flags);
3879 return config_output(ctx->outputs[0]);
3882 static av_cold void uninit(AVFilterContext *ctx)
3884 V360Context *s = ctx->priv;
3886 for (int p = 0; p < s->nb_allocated; p++) {
3889 av_freep(&s->ker[p]);
3894 static const AVFilterPad inputs[] = {
3897 .type = AVMEDIA_TYPE_VIDEO,
3898 .filter_frame = filter_frame,
3903 static const AVFilterPad outputs[] = {
3906 .type = AVMEDIA_TYPE_VIDEO,
3907 .config_props = config_output,
3912 AVFilter ff_vf_v360 = {
3914 .description = NULL_IF_CONFIG_SMALL("Convert 360 projection of video."),
3915 .priv_size = sizeof(V360Context),
3917 .query_formats = query_formats,
3920 .priv_class = &v360_class,
3921 .flags = AVFILTER_FLAG_SLICE_THREADS,
3922 .process_command = process_command,