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 * Reflect y operation.
628 * @param y input vertical position
629 * @param h input height
631 static inline int reflecty(int y, int h)
636 return 2 * h - 1 - y;
643 * Reflect x operation for equirect.
645 * @param x input horizontal position
646 * @param y input vertical position
647 * @param w input width
648 * @param h input height
650 static inline int ereflectx(int x, int y, int w, int h)
659 * Reflect x operation.
661 * @param x input horizontal position
662 * @param y input vertical position
663 * @param w input width
664 * @param h input height
666 static inline int reflectx(int x, int y, int w, int h)
675 * Convert char to corresponding direction.
676 * Used for cubemap options.
678 static int get_direction(char c)
699 * Convert char to corresponding rotation angle.
700 * Used for cubemap options.
702 static int get_rotation(char c)
719 * Convert char to corresponding rotation order.
721 static int get_rorder(char c)
739 * Prepare data for processing cubemap input format.
741 * @param ctx filter context
745 static int prepare_cube_in(AVFilterContext *ctx)
747 V360Context *s = ctx->priv;
749 for (int face = 0; face < NB_FACES; face++) {
750 const char c = s->in_forder[face];
754 av_log(ctx, AV_LOG_ERROR,
755 "Incomplete in_forder option. Direction for all 6 faces should be specified.\n");
756 return AVERROR(EINVAL);
759 direction = get_direction(c);
760 if (direction == -1) {
761 av_log(ctx, AV_LOG_ERROR,
762 "Incorrect direction symbol '%c' in in_forder option.\n", c);
763 return AVERROR(EINVAL);
766 s->in_cubemap_face_order[direction] = face;
769 for (int face = 0; face < NB_FACES; face++) {
770 const char c = s->in_frot[face];
774 av_log(ctx, AV_LOG_ERROR,
775 "Incomplete in_frot option. Rotation for all 6 faces should be specified.\n");
776 return AVERROR(EINVAL);
779 rotation = get_rotation(c);
780 if (rotation == -1) {
781 av_log(ctx, AV_LOG_ERROR,
782 "Incorrect rotation symbol '%c' in in_frot option.\n", c);
783 return AVERROR(EINVAL);
786 s->in_cubemap_face_rotation[face] = rotation;
793 * Prepare data for processing cubemap output format.
795 * @param ctx filter context
799 static int prepare_cube_out(AVFilterContext *ctx)
801 V360Context *s = ctx->priv;
803 for (int face = 0; face < NB_FACES; face++) {
804 const char c = s->out_forder[face];
808 av_log(ctx, AV_LOG_ERROR,
809 "Incomplete out_forder option. Direction for all 6 faces should be specified.\n");
810 return AVERROR(EINVAL);
813 direction = get_direction(c);
814 if (direction == -1) {
815 av_log(ctx, AV_LOG_ERROR,
816 "Incorrect direction symbol '%c' in out_forder option.\n", c);
817 return AVERROR(EINVAL);
820 s->out_cubemap_direction_order[face] = direction;
823 for (int face = 0; face < NB_FACES; face++) {
824 const char c = s->out_frot[face];
828 av_log(ctx, AV_LOG_ERROR,
829 "Incomplete out_frot option. Rotation for all 6 faces should be specified.\n");
830 return AVERROR(EINVAL);
833 rotation = get_rotation(c);
834 if (rotation == -1) {
835 av_log(ctx, AV_LOG_ERROR,
836 "Incorrect rotation symbol '%c' in out_frot option.\n", c);
837 return AVERROR(EINVAL);
840 s->out_cubemap_face_rotation[face] = rotation;
846 static inline void rotate_cube_face(float *uf, float *vf, int rotation)
872 static inline void rotate_cube_face_inverse(float *uf, float *vf, int rotation)
903 static void normalize_vector(float *vec)
905 const float norm = sqrtf(vec[0] * vec[0] + vec[1] * vec[1] + vec[2] * vec[2]);
913 * Calculate 3D coordinates on sphere for corresponding cubemap position.
914 * Common operation for every cubemap.
916 * @param s filter private context
917 * @param uf horizontal cubemap coordinate [0, 1)
918 * @param vf vertical cubemap coordinate [0, 1)
919 * @param face face of cubemap
920 * @param vec coordinates on sphere
921 * @param scalew scale for uf
922 * @param scaleh scale for vf
924 static void cube_to_xyz(const V360Context *s,
925 float uf, float vf, int face,
926 float *vec, float scalew, float scaleh)
928 const int direction = s->out_cubemap_direction_order[face];
934 rotate_cube_face_inverse(&uf, &vf, s->out_cubemap_face_rotation[face]);
975 normalize_vector(vec);
979 * Calculate cubemap position for corresponding 3D coordinates on sphere.
980 * Common operation for every cubemap.
982 * @param s filter private context
983 * @param vec coordinated on sphere
984 * @param uf horizontal cubemap coordinate [0, 1)
985 * @param vf vertical cubemap coordinate [0, 1)
986 * @param direction direction of view
988 static void xyz_to_cube(const V360Context *s,
990 float *uf, float *vf, int *direction)
992 const float phi = atan2f(vec[0], -vec[2]);
993 const float theta = asinf(-vec[1]);
994 float phi_norm, theta_threshold;
997 if (phi >= -M_PI_4 && phi < M_PI_4) {
1000 } else if (phi >= -(M_PI_2 + M_PI_4) && phi < -M_PI_4) {
1002 phi_norm = phi + M_PI_2;
1003 } else if (phi >= M_PI_4 && phi < M_PI_2 + M_PI_4) {
1005 phi_norm = phi - M_PI_2;
1008 phi_norm = phi + ((phi > 0.f) ? -M_PI : M_PI);
1011 theta_threshold = atanf(cosf(phi_norm));
1012 if (theta > theta_threshold) {
1014 } else if (theta < -theta_threshold) {
1018 switch (*direction) {
1020 *uf = vec[2] / vec[0];
1021 *vf = -vec[1] / vec[0];
1024 *uf = vec[2] / vec[0];
1025 *vf = vec[1] / vec[0];
1028 *uf = vec[0] / vec[1];
1029 *vf = -vec[2] / vec[1];
1032 *uf = -vec[0] / vec[1];
1033 *vf = -vec[2] / vec[1];
1036 *uf = -vec[0] / vec[2];
1037 *vf = vec[1] / vec[2];
1040 *uf = -vec[0] / vec[2];
1041 *vf = -vec[1] / vec[2];
1047 face = s->in_cubemap_face_order[*direction];
1048 rotate_cube_face(uf, vf, s->in_cubemap_face_rotation[face]);
1050 (*uf) *= s->input_mirror_modifier[0];
1051 (*vf) *= s->input_mirror_modifier[1];
1055 * Find position on another cube face in case of overflow/underflow.
1056 * Used for calculation of interpolation window.
1058 * @param s filter private context
1059 * @param uf horizontal cubemap coordinate
1060 * @param vf vertical cubemap coordinate
1061 * @param direction direction of view
1062 * @param new_uf new horizontal cubemap coordinate
1063 * @param new_vf new vertical cubemap coordinate
1064 * @param face face position on cubemap
1066 static void process_cube_coordinates(const V360Context *s,
1067 float uf, float vf, int direction,
1068 float *new_uf, float *new_vf, int *face)
1071 * Cubemap orientation
1078 * +-------+-------+-------+-------+ ^ e |
1080 * | left | front | right | back | | g |
1081 * +-------+-------+-------+-------+ v h v
1087 *face = s->in_cubemap_face_order[direction];
1088 rotate_cube_face_inverse(&uf, &vf, s->in_cubemap_face_rotation[*face]);
1090 if ((uf < -1.f || uf >= 1.f) && (vf < -1.f || vf >= 1.f)) {
1091 // There are no pixels to use in this case
1094 } else if (uf < -1.f) {
1096 switch (direction) {
1130 } else if (uf >= 1.f) {
1132 switch (direction) {
1166 } else if (vf < -1.f) {
1168 switch (direction) {
1202 } else if (vf >= 1.f) {
1204 switch (direction) {
1244 *face = s->in_cubemap_face_order[direction];
1245 rotate_cube_face(new_uf, new_vf, s->in_cubemap_face_rotation[*face]);
1249 * Calculate 3D coordinates on sphere for corresponding frame position in cubemap3x2 format.
1251 * @param s filter private context
1252 * @param i horizontal position on frame [0, width)
1253 * @param j vertical position on frame [0, height)
1254 * @param width frame width
1255 * @param height frame height
1256 * @param vec coordinates on sphere
1258 static int cube3x2_to_xyz(const V360Context *s,
1259 int i, int j, int width, int height,
1262 const float scalew = s->fout_pad > 0 ? 1.f - s->fout_pad / (s->out_width / 3.f) : 1.f - s->out_pad;
1263 const float scaleh = s->fout_pad > 0 ? 1.f - s->fout_pad / (s->out_height / 2.f) : 1.f - s->out_pad;
1265 const float ew = width / 3.f;
1266 const float eh = height / 2.f;
1268 const int u_face = floorf(i / ew);
1269 const int v_face = floorf(j / eh);
1270 const int face = u_face + 3 * v_face;
1272 const int u_shift = ceilf(ew * u_face);
1273 const int v_shift = ceilf(eh * v_face);
1274 const int ewi = ceilf(ew * (u_face + 1)) - u_shift;
1275 const int ehi = ceilf(eh * (v_face + 1)) - v_shift;
1277 const float uf = 2.f * (i - u_shift + 0.5f) / ewi - 1.f;
1278 const float vf = 2.f * (j - v_shift + 0.5f) / ehi - 1.f;
1280 cube_to_xyz(s, uf, vf, face, vec, scalew, scaleh);
1286 * Calculate frame position in cubemap3x2 format for corresponding 3D coordinates on sphere.
1288 * @param s filter private context
1289 * @param vec coordinates on sphere
1290 * @param width frame width
1291 * @param height frame height
1292 * @param us horizontal coordinates for interpolation window
1293 * @param vs vertical coordinates for interpolation window
1294 * @param du horizontal relative coordinate
1295 * @param dv vertical relative coordinate
1297 static int xyz_to_cube3x2(const V360Context *s,
1298 const float *vec, int width, int height,
1299 int16_t us[4][4], int16_t vs[4][4], float *du, float *dv)
1301 const float scalew = s->fin_pad > 0 ? 1.f - s->fin_pad / (s->in_width / 3.f) : 1.f - s->in_pad;
1302 const float scaleh = s->fin_pad > 0 ? 1.f - s->fin_pad / (s->in_height / 2.f) : 1.f - s->in_pad;
1303 const float ew = width / 3.f;
1304 const float eh = height / 2.f;
1308 int direction, face;
1311 xyz_to_cube(s, vec, &uf, &vf, &direction);
1316 face = s->in_cubemap_face_order[direction];
1319 ewi = ceilf(ew * (u_face + 1)) - ceilf(ew * u_face);
1320 ehi = ceilf(eh * (v_face + 1)) - ceilf(eh * v_face);
1322 uf = 0.5f * ewi * (uf + 1.f) - 0.5f;
1323 vf = 0.5f * ehi * (vf + 1.f) - 0.5f;
1331 for (int i = 0; i < 4; i++) {
1332 for (int j = 0; j < 4; j++) {
1333 int new_ui = ui + j - 1;
1334 int new_vi = vi + i - 1;
1335 int u_shift, v_shift;
1336 int new_ewi, new_ehi;
1338 if (new_ui >= 0 && new_ui < ewi && new_vi >= 0 && new_vi < ehi) {
1339 face = s->in_cubemap_face_order[direction];
1343 u_shift = ceilf(ew * u_face);
1344 v_shift = ceilf(eh * v_face);
1346 uf = 2.f * new_ui / ewi - 1.f;
1347 vf = 2.f * new_vi / ehi - 1.f;
1352 process_cube_coordinates(s, uf, vf, direction, &uf, &vf, &face);
1359 u_shift = ceilf(ew * u_face);
1360 v_shift = ceilf(eh * v_face);
1361 new_ewi = ceilf(ew * (u_face + 1)) - u_shift;
1362 new_ehi = ceilf(eh * (v_face + 1)) - v_shift;
1364 new_ui = av_clip(lrintf(0.5f * new_ewi * (uf + 1.f)), 0, new_ewi - 1);
1365 new_vi = av_clip(lrintf(0.5f * new_ehi * (vf + 1.f)), 0, new_ehi - 1);
1368 us[i][j] = u_shift + new_ui;
1369 vs[i][j] = v_shift + new_vi;
1377 * Calculate 3D coordinates on sphere for corresponding frame position in cubemap1x6 format.
1379 * @param s filter private context
1380 * @param i horizontal position on frame [0, width)
1381 * @param j vertical position on frame [0, height)
1382 * @param width frame width
1383 * @param height frame height
1384 * @param vec coordinates on sphere
1386 static int cube1x6_to_xyz(const V360Context *s,
1387 int i, int j, int width, int height,
1390 const float scalew = s->fout_pad > 0 ? 1.f - (float)(s->fout_pad) / s->out_width : 1.f - s->out_pad;
1391 const float scaleh = s->fout_pad > 0 ? 1.f - s->fout_pad / (s->out_height / 6.f) : 1.f - s->out_pad;
1393 const float ew = width;
1394 const float eh = height / 6.f;
1396 const int face = floorf(j / eh);
1398 const int v_shift = ceilf(eh * face);
1399 const int ehi = ceilf(eh * (face + 1)) - v_shift;
1401 const float uf = 2.f * (i + 0.5f) / ew - 1.f;
1402 const float vf = 2.f * (j - v_shift + 0.5f) / ehi - 1.f;
1404 cube_to_xyz(s, uf, vf, face, vec, scalew, scaleh);
1410 * Calculate 3D coordinates on sphere for corresponding frame position in cubemap6x1 format.
1412 * @param s filter private context
1413 * @param i horizontal position on frame [0, width)
1414 * @param j vertical position on frame [0, height)
1415 * @param width frame width
1416 * @param height frame height
1417 * @param vec coordinates on sphere
1419 static int cube6x1_to_xyz(const V360Context *s,
1420 int i, int j, int width, int height,
1423 const float scalew = s->fout_pad > 0 ? 1.f - s->fout_pad / (s->out_width / 6.f) : 1.f - s->out_pad;
1424 const float scaleh = s->fout_pad > 0 ? 1.f - (float)(s->fout_pad) / s->out_height : 1.f - s->out_pad;
1426 const float ew = width / 6.f;
1427 const float eh = height;
1429 const int face = floorf(i / ew);
1431 const int u_shift = ceilf(ew * face);
1432 const int ewi = ceilf(ew * (face + 1)) - u_shift;
1434 const float uf = 2.f * (i - u_shift + 0.5f) / ewi - 1.f;
1435 const float vf = 2.f * (j + 0.5f) / eh - 1.f;
1437 cube_to_xyz(s, uf, vf, face, vec, scalew, scaleh);
1443 * Calculate frame position in cubemap1x6 format for corresponding 3D coordinates on sphere.
1445 * @param s filter private context
1446 * @param vec coordinates on sphere
1447 * @param width frame width
1448 * @param height frame height
1449 * @param us horizontal coordinates for interpolation window
1450 * @param vs vertical coordinates for interpolation window
1451 * @param du horizontal relative coordinate
1452 * @param dv vertical relative coordinate
1454 static int xyz_to_cube1x6(const V360Context *s,
1455 const float *vec, int width, int height,
1456 int16_t us[4][4], int16_t vs[4][4], float *du, float *dv)
1458 const float scalew = s->fin_pad > 0 ? 1.f - (float)(s->fin_pad) / s->in_width : 1.f - s->in_pad;
1459 const float scaleh = s->fin_pad > 0 ? 1.f - s->fin_pad / (s->in_height / 6.f) : 1.f - s->in_pad;
1460 const float eh = height / 6.f;
1461 const int ewi = width;
1465 int direction, face;
1467 xyz_to_cube(s, vec, &uf, &vf, &direction);
1472 face = s->in_cubemap_face_order[direction];
1473 ehi = ceilf(eh * (face + 1)) - ceilf(eh * face);
1475 uf = 0.5f * ewi * (uf + 1.f) - 0.5f;
1476 vf = 0.5f * ehi * (vf + 1.f) - 0.5f;
1484 for (int i = 0; i < 4; i++) {
1485 for (int j = 0; j < 4; j++) {
1486 int new_ui = ui + j - 1;
1487 int new_vi = vi + i - 1;
1491 if (new_ui >= 0 && new_ui < ewi && new_vi >= 0 && new_vi < ehi) {
1492 face = s->in_cubemap_face_order[direction];
1494 v_shift = ceilf(eh * face);
1496 uf = 2.f * new_ui / ewi - 1.f;
1497 vf = 2.f * new_vi / ehi - 1.f;
1502 process_cube_coordinates(s, uf, vf, direction, &uf, &vf, &face);
1507 v_shift = ceilf(eh * face);
1508 new_ehi = ceilf(eh * (face + 1)) - v_shift;
1510 new_ui = av_clip(lrintf(0.5f * ewi * (uf + 1.f)), 0, ewi - 1);
1511 new_vi = av_clip(lrintf(0.5f * new_ehi * (vf + 1.f)), 0, new_ehi - 1);
1515 vs[i][j] = v_shift + new_vi;
1523 * Calculate frame position in cubemap6x1 format for corresponding 3D coordinates on sphere.
1525 * @param s filter private context
1526 * @param vec coordinates on sphere
1527 * @param width frame width
1528 * @param height frame height
1529 * @param us horizontal coordinates for interpolation window
1530 * @param vs vertical coordinates for interpolation window
1531 * @param du horizontal relative coordinate
1532 * @param dv vertical relative coordinate
1534 static int xyz_to_cube6x1(const V360Context *s,
1535 const float *vec, int width, int height,
1536 int16_t us[4][4], int16_t vs[4][4], float *du, float *dv)
1538 const float scalew = s->fin_pad > 0 ? 1.f - s->fin_pad / (s->in_width / 6.f) : 1.f - s->in_pad;
1539 const float scaleh = s->fin_pad > 0 ? 1.f - (float)(s->fin_pad) / s->in_height : 1.f - s->in_pad;
1540 const float ew = width / 6.f;
1541 const int ehi = height;
1545 int direction, face;
1547 xyz_to_cube(s, vec, &uf, &vf, &direction);
1552 face = s->in_cubemap_face_order[direction];
1553 ewi = ceilf(ew * (face + 1)) - ceilf(ew * face);
1555 uf = 0.5f * ewi * (uf + 1.f) - 0.5f;
1556 vf = 0.5f * ehi * (vf + 1.f) - 0.5f;
1564 for (int i = 0; i < 4; i++) {
1565 for (int j = 0; j < 4; j++) {
1566 int new_ui = ui + j - 1;
1567 int new_vi = vi + i - 1;
1571 if (new_ui >= 0 && new_ui < ewi && new_vi >= 0 && new_vi < ehi) {
1572 face = s->in_cubemap_face_order[direction];
1574 u_shift = ceilf(ew * face);
1576 uf = 2.f * new_ui / ewi - 1.f;
1577 vf = 2.f * new_vi / ehi - 1.f;
1582 process_cube_coordinates(s, uf, vf, direction, &uf, &vf, &face);
1587 u_shift = ceilf(ew * face);
1588 new_ewi = ceilf(ew * (face + 1)) - u_shift;
1590 new_ui = av_clip(lrintf(0.5f * new_ewi * (uf + 1.f)), 0, new_ewi - 1);
1591 new_vi = av_clip(lrintf(0.5f * ehi * (vf + 1.f)), 0, ehi - 1);
1594 us[i][j] = u_shift + new_ui;
1603 * Calculate 3D coordinates on sphere for corresponding frame position in equirectangular format.
1605 * @param s filter private context
1606 * @param i horizontal position on frame [0, width)
1607 * @param j vertical position on frame [0, height)
1608 * @param width frame width
1609 * @param height frame height
1610 * @param vec coordinates on sphere
1612 static int equirect_to_xyz(const V360Context *s,
1613 int i, int j, int width, int height,
1616 const float phi = ((2.f * i + 0.5f) / width - 1.f) * M_PI;
1617 const float theta = ((2.f * j + 0.5f) / height - 1.f) * M_PI_2;
1619 const float sin_phi = sinf(phi);
1620 const float cos_phi = cosf(phi);
1621 const float sin_theta = sinf(theta);
1622 const float cos_theta = cosf(theta);
1624 vec[0] = cos_theta * sin_phi;
1625 vec[1] = -sin_theta;
1626 vec[2] = -cos_theta * cos_phi;
1632 * Prepare data for processing stereographic output format.
1634 * @param ctx filter context
1636 * @return error code
1638 static int prepare_stereographic_out(AVFilterContext *ctx)
1640 V360Context *s = ctx->priv;
1642 s->flat_range[0] = tanf(FFMIN(s->h_fov, 359.f) * M_PI / 720.f);
1643 s->flat_range[1] = tanf(FFMIN(s->v_fov, 359.f) * M_PI / 720.f);
1649 * Calculate 3D coordinates on sphere for corresponding frame position in stereographic format.
1651 * @param s filter private context
1652 * @param i horizontal position on frame [0, width)
1653 * @param j vertical position on frame [0, height)
1654 * @param width frame width
1655 * @param height frame height
1656 * @param vec coordinates on sphere
1658 static int stereographic_to_xyz(const V360Context *s,
1659 int i, int j, int width, int height,
1662 const float x = ((2.f * i + 1.f) / width - 1.f) * s->flat_range[0];
1663 const float y = ((2.f * j + 1.f) / height - 1.f) * s->flat_range[1];
1664 const float xy = x * x + y * y;
1666 vec[0] = 2.f * x / (1.f + xy);
1667 vec[1] = (-1.f + xy) / (1.f + xy);
1668 vec[2] = 2.f * y / (1.f + xy);
1670 normalize_vector(vec);
1676 * Prepare data for processing stereographic input format.
1678 * @param ctx filter context
1680 * @return error code
1682 static int prepare_stereographic_in(AVFilterContext *ctx)
1684 V360Context *s = ctx->priv;
1686 s->iflat_range[0] = tanf(FFMIN(s->ih_fov, 359.f) * M_PI / 720.f);
1687 s->iflat_range[1] = tanf(FFMIN(s->iv_fov, 359.f) * M_PI / 720.f);
1693 * Calculate frame position in stereographic format for corresponding 3D coordinates on sphere.
1695 * @param s filter private context
1696 * @param vec coordinates on sphere
1697 * @param width frame width
1698 * @param height frame height
1699 * @param us horizontal coordinates for interpolation window
1700 * @param vs vertical coordinates for interpolation window
1701 * @param du horizontal relative coordinate
1702 * @param dv vertical relative coordinate
1704 static int xyz_to_stereographic(const V360Context *s,
1705 const float *vec, int width, int height,
1706 int16_t us[4][4], int16_t vs[4][4], float *du, float *dv)
1708 const float x = vec[0] / (1.f - vec[1]) / s->iflat_range[0] * s->input_mirror_modifier[0];
1709 const float y = vec[2] / (1.f - vec[1]) / s->iflat_range[1] * s->input_mirror_modifier[1];
1711 const float uf = (x + 1.f) * width / 2.f;
1712 const float vf = (y + 1.f) * height / 2.f;
1714 const int ui = floorf(uf);
1715 const int vi = floorf(vf);
1717 const int visible = isfinite(x) && isfinite(y) && vi >= 0 && vi < height && ui >= 0 && ui < width;
1719 *du = visible ? uf - ui : 0.f;
1720 *dv = visible ? vf - vi : 0.f;
1722 for (int i = 0; i < 4; i++) {
1723 for (int j = 0; j < 4; j++) {
1724 us[i][j] = visible ? av_clip(ui + j - 1, 0, width - 1) : 0;
1725 vs[i][j] = visible ? av_clip(vi + i - 1, 0, height - 1) : 0;
1733 * Calculate frame position in equirectangular format for corresponding 3D coordinates on sphere.
1735 * @param s filter private context
1736 * @param vec coordinates on sphere
1737 * @param width frame width
1738 * @param height frame height
1739 * @param us horizontal coordinates for interpolation window
1740 * @param vs vertical coordinates for interpolation window
1741 * @param du horizontal relative coordinate
1742 * @param dv vertical relative coordinate
1744 static int xyz_to_equirect(const V360Context *s,
1745 const float *vec, int width, int height,
1746 int16_t us[4][4], int16_t vs[4][4], float *du, float *dv)
1748 const float phi = atan2f(vec[0], -vec[2]) * s->input_mirror_modifier[0];
1749 const float theta = asinf(-vec[1]) * s->input_mirror_modifier[1];
1751 const float uf = (phi / M_PI + 1.f) * width / 2.f;
1752 const float vf = (theta / M_PI_2 + 1.f) * height / 2.f;
1754 const int ui = floorf(uf);
1755 const int vi = floorf(vf);
1760 for (int i = 0; i < 4; i++) {
1761 for (int j = 0; j < 4; j++) {
1762 us[i][j] = ereflectx(ui + j - 1, vi + i - 1, width, height);
1763 vs[i][j] = reflecty(vi + i - 1, height);
1771 * Prepare data for processing flat input format.
1773 * @param ctx filter context
1775 * @return error code
1777 static int prepare_flat_in(AVFilterContext *ctx)
1779 V360Context *s = ctx->priv;
1781 s->iflat_range[0] = tanf(0.5f * s->ih_fov * M_PI / 180.f);
1782 s->iflat_range[1] = tanf(0.5f * s->iv_fov * M_PI / 180.f);
1788 * Calculate frame position in flat 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_flat(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 theta = acosf(vec[2]);
1804 const float r = tanf(theta);
1805 const float rr = fabsf(r) < 1e+6f ? r : hypotf(width, height);
1806 const float zf = -vec[2];
1807 const float h = hypotf(vec[0], vec[1]);
1808 const float c = h <= 1e-6f ? 1.f : rr / h;
1809 float uf = -vec[0] * c / s->iflat_range[0] * s->input_mirror_modifier[0];
1810 float vf = vec[1] * c / s->iflat_range[1] * s->input_mirror_modifier[1];
1811 int visible, ui, vi;
1813 uf = zf >= 0.f ? (uf + 1.f) * width / 2.f : 0.f;
1814 vf = zf >= 0.f ? (vf + 1.f) * height / 2.f : 0.f;
1819 visible = vi >= 0 && vi < height && ui >= 0 && ui < width && zf >= 0.f;
1824 for (int i = 0; i < 4; i++) {
1825 for (int j = 0; j < 4; j++) {
1826 us[i][j] = visible ? av_clip(ui + j - 1, 0, width - 1) : 0;
1827 vs[i][j] = visible ? av_clip(vi + i - 1, 0, height - 1) : 0;
1835 * Calculate frame position in mercator format for corresponding 3D coordinates on sphere.
1837 * @param s filter private context
1838 * @param vec coordinates on sphere
1839 * @param width frame width
1840 * @param height frame height
1841 * @param us horizontal coordinates for interpolation window
1842 * @param vs vertical coordinates for interpolation window
1843 * @param du horizontal relative coordinate
1844 * @param dv vertical relative coordinate
1846 static int xyz_to_mercator(const V360Context *s,
1847 const float *vec, int width, int height,
1848 int16_t us[4][4], int16_t vs[4][4], float *du, float *dv)
1850 const float phi = atan2f(vec[0], -vec[2]) * s->input_mirror_modifier[0];
1851 const float theta = -vec[1] * s->input_mirror_modifier[1];
1853 const float uf = (phi / M_PI + 1.f) * width / 2.f;
1854 const float vf = (av_clipf(logf((1.f + theta) / (1.f - theta)) / (2.f * M_PI), -1.f, 1.f) + 1.f) * height / 2.f;
1856 const int ui = floorf(uf);
1857 const int vi = floorf(vf);
1862 for (int i = 0; i < 4; i++) {
1863 for (int j = 0; j < 4; j++) {
1864 us[i][j] = av_clip(ui + j - 1, 0, width - 1);
1865 vs[i][j] = av_clip(vi + i - 1, 0, height - 1);
1873 * Calculate 3D coordinates on sphere for corresponding frame position in mercator format.
1875 * @param s filter private context
1876 * @param i horizontal position on frame [0, width)
1877 * @param j vertical position on frame [0, height)
1878 * @param width frame width
1879 * @param height frame height
1880 * @param vec coordinates on sphere
1882 static int mercator_to_xyz(const V360Context *s,
1883 int i, int j, int width, int height,
1886 const float phi = ((2.f * i + 1.f) / width - 1.f) * M_PI + M_PI_2;
1887 const float y = ((2.f * j + 1.f) / height - 1.f) * M_PI;
1888 const float div = expf(2.f * y) + 1.f;
1890 const float sin_phi = sinf(phi);
1891 const float cos_phi = cosf(phi);
1892 const float sin_theta = -2.f * expf(y) / div;
1893 const float cos_theta = -(expf(2.f * y) - 1.f) / div;
1895 vec[0] = sin_theta * cos_phi;
1897 vec[2] = sin_theta * sin_phi;
1903 * Calculate frame position in ball format for corresponding 3D coordinates on sphere.
1905 * @param s filter private context
1906 * @param vec coordinates on sphere
1907 * @param width frame width
1908 * @param height frame height
1909 * @param us horizontal coordinates for interpolation window
1910 * @param vs vertical coordinates for interpolation window
1911 * @param du horizontal relative coordinate
1912 * @param dv vertical relative coordinate
1914 static int xyz_to_ball(const V360Context *s,
1915 const float *vec, int width, int height,
1916 int16_t us[4][4], int16_t vs[4][4], float *du, float *dv)
1918 const float l = hypotf(vec[0], vec[1]);
1919 const float r = sqrtf(1.f + vec[2]) / M_SQRT2;
1921 const float uf = (1.f + r * vec[0] * s->input_mirror_modifier[0] / (l > 0.f ? l : 1.f)) * width * 0.5f;
1922 const float vf = (1.f - r * vec[1] * s->input_mirror_modifier[1] / (l > 0.f ? l : 1.f)) * height * 0.5f;
1924 const int ui = floorf(uf);
1925 const int vi = floorf(vf);
1930 for (int i = 0; i < 4; i++) {
1931 for (int j = 0; j < 4; j++) {
1932 us[i][j] = av_clip(ui + j - 1, 0, width - 1);
1933 vs[i][j] = av_clip(vi + i - 1, 0, height - 1);
1941 * Calculate 3D coordinates on sphere for corresponding frame position in ball format.
1943 * @param s filter private context
1944 * @param i horizontal position on frame [0, width)
1945 * @param j vertical position on frame [0, height)
1946 * @param width frame width
1947 * @param height frame height
1948 * @param vec coordinates on sphere
1950 static int ball_to_xyz(const V360Context *s,
1951 int i, int j, int width, int height,
1954 const float x = (2.f * i + 1.f) / width - 1.f;
1955 const float y = (2.f * j + 1.f) / height - 1.f;
1956 const float l = hypotf(x, y);
1959 const float z = 2.f * l * sqrtf(1.f - l * l);
1961 vec[0] = z * x / (l > 0.f ? l : 1.f);
1962 vec[1] = -z * y / (l > 0.f ? l : 1.f);
1963 vec[2] = -1.f + 2.f * l * l;
1975 * Calculate 3D coordinates on sphere for corresponding frame position in hammer format.
1977 * @param s filter private context
1978 * @param i horizontal position on frame [0, width)
1979 * @param j vertical position on frame [0, height)
1980 * @param width frame width
1981 * @param height frame height
1982 * @param vec coordinates on sphere
1984 static int hammer_to_xyz(const V360Context *s,
1985 int i, int j, int width, int height,
1988 const float x = ((2.f * i + 1.f) / width - 1.f);
1989 const float y = ((2.f * j + 1.f) / height - 1.f);
1991 const float xx = x * x;
1992 const float yy = y * y;
1994 const float z = sqrtf(1.f - xx * 0.5f - yy * 0.5f);
1996 const float a = M_SQRT2 * x * z;
1997 const float b = 2.f * z * z - 1.f;
1999 const float aa = a * a;
2000 const float bb = b * b;
2002 const float w = sqrtf(1.f - 2.f * yy * z * z);
2004 vec[0] = w * 2.f * a * b / (aa + bb);
2005 vec[1] = -M_SQRT2 * y * z;
2006 vec[2] = -w * (bb - aa) / (aa + bb);
2008 normalize_vector(vec);
2014 * Calculate frame position in hammer format for corresponding 3D coordinates on sphere.
2016 * @param s filter private context
2017 * @param vec coordinates on sphere
2018 * @param width frame width
2019 * @param height frame height
2020 * @param us horizontal coordinates for interpolation window
2021 * @param vs vertical coordinates for interpolation window
2022 * @param du horizontal relative coordinate
2023 * @param dv vertical relative coordinate
2025 static int xyz_to_hammer(const V360Context *s,
2026 const float *vec, int width, int height,
2027 int16_t us[4][4], int16_t vs[4][4], float *du, float *dv)
2029 const float theta = atan2f(vec[0], -vec[2]) * s->input_mirror_modifier[0];
2031 const float z = sqrtf(1.f + sqrtf(1.f - vec[1] * vec[1]) * cosf(theta * 0.5f));
2032 const float x = sqrtf(1.f - vec[1] * vec[1]) * sinf(theta * 0.5f) / z;
2033 const float y = -vec[1] / z * s->input_mirror_modifier[1];
2035 const float uf = (x + 1.f) * width / 2.f;
2036 const float vf = (y + 1.f) * height / 2.f;
2038 const int ui = floorf(uf);
2039 const int vi = floorf(vf);
2044 for (int i = 0; i < 4; i++) {
2045 for (int j = 0; j < 4; j++) {
2046 us[i][j] = av_clip(ui + j - 1, 0, width - 1);
2047 vs[i][j] = av_clip(vi + i - 1, 0, height - 1);
2055 * Calculate 3D coordinates on sphere for corresponding frame position in sinusoidal format.
2057 * @param s filter private context
2058 * @param i horizontal position on frame [0, width)
2059 * @param j vertical position on frame [0, height)
2060 * @param width frame width
2061 * @param height frame height
2062 * @param vec coordinates on sphere
2064 static int sinusoidal_to_xyz(const V360Context *s,
2065 int i, int j, int width, int height,
2068 const float theta = ((2.f * j + 1.f) / height - 1.f) * M_PI_2;
2069 const float phi = ((2.f * i + 1.f) / width - 1.f) * M_PI / cosf(theta);
2071 const float sin_phi = sinf(phi);
2072 const float cos_phi = cosf(phi);
2073 const float sin_theta = sinf(theta);
2074 const float cos_theta = cosf(theta);
2076 vec[0] = cos_theta * sin_phi;
2077 vec[1] = -sin_theta;
2078 vec[2] = -cos_theta * cos_phi;
2080 normalize_vector(vec);
2086 * Calculate frame position in sinusoidal format for corresponding 3D coordinates on sphere.
2088 * @param s filter private context
2089 * @param vec coordinates on sphere
2090 * @param width frame width
2091 * @param height frame height
2092 * @param us horizontal coordinates for interpolation window
2093 * @param vs vertical coordinates for interpolation window
2094 * @param du horizontal relative coordinate
2095 * @param dv vertical relative coordinate
2097 static int xyz_to_sinusoidal(const V360Context *s,
2098 const float *vec, int width, int height,
2099 int16_t us[4][4], int16_t vs[4][4], float *du, float *dv)
2101 const float theta = asinf(-vec[1]) * s->input_mirror_modifier[1];
2102 const float phi = atan2f(vec[0], -vec[2]) * s->input_mirror_modifier[0] * cosf(theta);
2104 const float uf = (phi / M_PI + 1.f) * width / 2.f;
2105 const float vf = (theta / M_PI_2 + 1.f) * height / 2.f;
2107 const int ui = floorf(uf);
2108 const int vi = floorf(vf);
2113 for (int i = 0; i < 4; i++) {
2114 for (int j = 0; j < 4; j++) {
2115 us[i][j] = av_clip(ui + j - 1, 0, width - 1);
2116 vs[i][j] = av_clip(vi + i - 1, 0, height - 1);
2124 * Prepare data for processing equi-angular cubemap input format.
2126 * @param ctx filter context
2128 * @return error code
2130 static int prepare_eac_in(AVFilterContext *ctx)
2132 V360Context *s = ctx->priv;
2134 if (s->ih_flip && s->iv_flip) {
2135 s->in_cubemap_face_order[RIGHT] = BOTTOM_LEFT;
2136 s->in_cubemap_face_order[LEFT] = BOTTOM_RIGHT;
2137 s->in_cubemap_face_order[UP] = TOP_LEFT;
2138 s->in_cubemap_face_order[DOWN] = TOP_RIGHT;
2139 s->in_cubemap_face_order[FRONT] = BOTTOM_MIDDLE;
2140 s->in_cubemap_face_order[BACK] = TOP_MIDDLE;
2141 } else if (s->ih_flip) {
2142 s->in_cubemap_face_order[RIGHT] = TOP_LEFT;
2143 s->in_cubemap_face_order[LEFT] = TOP_RIGHT;
2144 s->in_cubemap_face_order[UP] = BOTTOM_LEFT;
2145 s->in_cubemap_face_order[DOWN] = BOTTOM_RIGHT;
2146 s->in_cubemap_face_order[FRONT] = TOP_MIDDLE;
2147 s->in_cubemap_face_order[BACK] = BOTTOM_MIDDLE;
2148 } else if (s->iv_flip) {
2149 s->in_cubemap_face_order[RIGHT] = BOTTOM_RIGHT;
2150 s->in_cubemap_face_order[LEFT] = BOTTOM_LEFT;
2151 s->in_cubemap_face_order[UP] = TOP_RIGHT;
2152 s->in_cubemap_face_order[DOWN] = TOP_LEFT;
2153 s->in_cubemap_face_order[FRONT] = BOTTOM_MIDDLE;
2154 s->in_cubemap_face_order[BACK] = TOP_MIDDLE;
2156 s->in_cubemap_face_order[RIGHT] = TOP_RIGHT;
2157 s->in_cubemap_face_order[LEFT] = TOP_LEFT;
2158 s->in_cubemap_face_order[UP] = BOTTOM_RIGHT;
2159 s->in_cubemap_face_order[DOWN] = BOTTOM_LEFT;
2160 s->in_cubemap_face_order[FRONT] = TOP_MIDDLE;
2161 s->in_cubemap_face_order[BACK] = BOTTOM_MIDDLE;
2165 s->in_cubemap_face_rotation[TOP_LEFT] = ROT_270;
2166 s->in_cubemap_face_rotation[TOP_MIDDLE] = ROT_90;
2167 s->in_cubemap_face_rotation[TOP_RIGHT] = ROT_270;
2168 s->in_cubemap_face_rotation[BOTTOM_LEFT] = ROT_0;
2169 s->in_cubemap_face_rotation[BOTTOM_MIDDLE] = ROT_0;
2170 s->in_cubemap_face_rotation[BOTTOM_RIGHT] = ROT_0;
2172 s->in_cubemap_face_rotation[TOP_LEFT] = ROT_0;
2173 s->in_cubemap_face_rotation[TOP_MIDDLE] = ROT_0;
2174 s->in_cubemap_face_rotation[TOP_RIGHT] = ROT_0;
2175 s->in_cubemap_face_rotation[BOTTOM_LEFT] = ROT_270;
2176 s->in_cubemap_face_rotation[BOTTOM_MIDDLE] = ROT_90;
2177 s->in_cubemap_face_rotation[BOTTOM_RIGHT] = ROT_270;
2184 * Prepare data for processing equi-angular cubemap output format.
2186 * @param ctx filter context
2188 * @return error code
2190 static int prepare_eac_out(AVFilterContext *ctx)
2192 V360Context *s = ctx->priv;
2194 s->out_cubemap_direction_order[TOP_LEFT] = LEFT;
2195 s->out_cubemap_direction_order[TOP_MIDDLE] = FRONT;
2196 s->out_cubemap_direction_order[TOP_RIGHT] = RIGHT;
2197 s->out_cubemap_direction_order[BOTTOM_LEFT] = DOWN;
2198 s->out_cubemap_direction_order[BOTTOM_MIDDLE] = BACK;
2199 s->out_cubemap_direction_order[BOTTOM_RIGHT] = UP;
2201 s->out_cubemap_face_rotation[TOP_LEFT] = ROT_0;
2202 s->out_cubemap_face_rotation[TOP_MIDDLE] = ROT_0;
2203 s->out_cubemap_face_rotation[TOP_RIGHT] = ROT_0;
2204 s->out_cubemap_face_rotation[BOTTOM_LEFT] = ROT_270;
2205 s->out_cubemap_face_rotation[BOTTOM_MIDDLE] = ROT_90;
2206 s->out_cubemap_face_rotation[BOTTOM_RIGHT] = ROT_270;
2212 * Calculate 3D coordinates on sphere for corresponding frame position in equi-angular cubemap format.
2214 * @param s filter private context
2215 * @param i horizontal position on frame [0, width)
2216 * @param j vertical position on frame [0, height)
2217 * @param width frame width
2218 * @param height frame height
2219 * @param vec coordinates on sphere
2221 static int eac_to_xyz(const V360Context *s,
2222 int i, int j, int width, int height,
2225 const float pixel_pad = 2;
2226 const float u_pad = pixel_pad / width;
2227 const float v_pad = pixel_pad / height;
2229 int u_face, v_face, face;
2231 float l_x, l_y, l_z;
2233 float uf = (i + 0.5f) / width;
2234 float vf = (j + 0.5f) / height;
2236 // EAC has 2-pixel padding on faces except between faces on the same row
2237 // Padding pixels seems not to be stretched with tangent as regular pixels
2238 // Formulas below approximate original padding as close as I could get experimentally
2240 // Horizontal padding
2241 uf = 3.f * (uf - u_pad) / (1.f - 2.f * u_pad);
2245 } else if (uf >= 3.f) {
2249 u_face = floorf(uf);
2250 uf = fmodf(uf, 1.f) - 0.5f;
2254 v_face = floorf(vf * 2.f);
2255 vf = (vf - v_pad - 0.5f * v_face) / (0.5f - 2.f * v_pad) - 0.5f;
2257 if (uf >= -0.5f && uf < 0.5f) {
2258 uf = tanf(M_PI_2 * uf);
2262 if (vf >= -0.5f && vf < 0.5f) {
2263 vf = tanf(M_PI_2 * vf);
2268 face = u_face + 3 * v_face;
2309 normalize_vector(vec);
2315 * Calculate frame position in equi-angular cubemap format for corresponding 3D coordinates on sphere.
2317 * @param s filter private context
2318 * @param vec coordinates on sphere
2319 * @param width frame width
2320 * @param height frame height
2321 * @param us horizontal coordinates for interpolation window
2322 * @param vs vertical coordinates for interpolation window
2323 * @param du horizontal relative coordinate
2324 * @param dv vertical relative coordinate
2326 static int xyz_to_eac(const V360Context *s,
2327 const float *vec, int width, int height,
2328 int16_t us[4][4], int16_t vs[4][4], float *du, float *dv)
2330 const float pixel_pad = 2;
2331 const float u_pad = pixel_pad / width;
2332 const float v_pad = pixel_pad / height;
2336 int direction, face;
2339 xyz_to_cube(s, vec, &uf, &vf, &direction);
2341 face = s->in_cubemap_face_order[direction];
2345 uf = M_2_PI * atanf(uf) + 0.5f;
2346 vf = M_2_PI * atanf(vf) + 0.5f;
2348 // These formulas are inversed from eac_to_xyz ones
2349 uf = (uf + u_face) * (1.f - 2.f * u_pad) / 3.f + u_pad;
2350 vf = vf * (0.5f - 2.f * v_pad) + v_pad + 0.5f * v_face;
2364 for (int i = 0; i < 4; i++) {
2365 for (int j = 0; j < 4; j++) {
2366 us[i][j] = av_clip(ui + j - 1, 0, width - 1);
2367 vs[i][j] = av_clip(vi + i - 1, 0, height - 1);
2375 * Prepare data for processing flat output format.
2377 * @param ctx filter context
2379 * @return error code
2381 static int prepare_flat_out(AVFilterContext *ctx)
2383 V360Context *s = ctx->priv;
2385 s->flat_range[0] = tanf(0.5f * s->h_fov * M_PI / 180.f);
2386 s->flat_range[1] = tanf(0.5f * s->v_fov * M_PI / 180.f);
2392 * Calculate 3D coordinates on sphere for corresponding frame position in flat format.
2394 * @param s filter private context
2395 * @param i horizontal position on frame [0, width)
2396 * @param j vertical position on frame [0, height)
2397 * @param width frame width
2398 * @param height frame height
2399 * @param vec coordinates on sphere
2401 static int flat_to_xyz(const V360Context *s,
2402 int i, int j, int width, int height,
2405 const float l_x = s->flat_range[0] * ((2.f * i + 0.5f) / width - 1.f);
2406 const float l_y = -s->flat_range[1] * ((2.f * j + 0.5f) / height - 1.f);
2412 normalize_vector(vec);
2418 * Prepare data for processing fisheye output format.
2420 * @param ctx filter context
2422 * @return error code
2424 static int prepare_fisheye_out(AVFilterContext *ctx)
2426 V360Context *s = ctx->priv;
2428 s->flat_range[0] = s->h_fov / 180.f;
2429 s->flat_range[1] = s->v_fov / 180.f;
2435 * Calculate 3D coordinates on sphere for corresponding frame position in fisheye format.
2437 * @param s filter private context
2438 * @param i horizontal position on frame [0, width)
2439 * @param j vertical position on frame [0, height)
2440 * @param width frame width
2441 * @param height frame height
2442 * @param vec coordinates on sphere
2444 static int fisheye_to_xyz(const V360Context *s,
2445 int i, int j, int width, int height,
2448 const float uf = s->flat_range[0] * ((2.f * i) / width - 1.f);
2449 const float vf = s->flat_range[1] * ((2.f * j + 1.f) / height - 1.f);
2451 const float phi = -atan2f(vf, uf);
2452 const float theta = -M_PI_2 * (1.f - hypotf(uf, vf));
2454 vec[0] = cosf(theta) * cosf(phi);
2455 vec[1] = cosf(theta) * sinf(phi);
2456 vec[2] = sinf(theta);
2458 normalize_vector(vec);
2464 * Prepare data for processing fisheye input format.
2466 * @param ctx filter context
2468 * @return error code
2470 static int prepare_fisheye_in(AVFilterContext *ctx)
2472 V360Context *s = ctx->priv;
2474 s->iflat_range[0] = s->ih_fov / 180.f;
2475 s->iflat_range[1] = s->iv_fov / 180.f;
2481 * Calculate frame position in fisheye format for corresponding 3D coordinates on sphere.
2483 * @param s filter private context
2484 * @param vec coordinates on sphere
2485 * @param width frame width
2486 * @param height frame height
2487 * @param us horizontal coordinates for interpolation window
2488 * @param vs vertical coordinates for interpolation window
2489 * @param du horizontal relative coordinate
2490 * @param dv vertical relative coordinate
2492 static int xyz_to_fisheye(const V360Context *s,
2493 const float *vec, int width, int height,
2494 int16_t us[4][4], int16_t vs[4][4], float *du, float *dv)
2496 const float h = hypotf(vec[0], vec[1]);
2497 const float lh = h > 0.f ? h : 1.f;
2498 const float phi = atan2f(h, -vec[2]) / M_PI;
2500 float uf = vec[0] / lh * phi * s->input_mirror_modifier[0] / s->iflat_range[0];
2501 float vf = -vec[1] / lh * phi * s->input_mirror_modifier[1] / s->iflat_range[1];
2503 const int visible = hypotf(uf, vf) <= 0.5f;
2506 uf = (uf + 0.5f) * width;
2507 vf = (vf + 0.5f) * height;
2512 *du = visible ? uf - ui : 0.f;
2513 *dv = visible ? vf - vi : 0.f;
2515 for (int i = 0; i < 4; i++) {
2516 for (int j = 0; j < 4; j++) {
2517 us[i][j] = visible ? av_clip(ui + j - 1, 0, width - 1) : 0;
2518 vs[i][j] = visible ? av_clip(vi + i - 1, 0, height - 1) : 0;
2526 * Calculate 3D coordinates on sphere for corresponding frame position in pannini format.
2528 * @param s filter private context
2529 * @param i horizontal position on frame [0, width)
2530 * @param j vertical position on frame [0, height)
2531 * @param width frame width
2532 * @param height frame height
2533 * @param vec coordinates on sphere
2535 static int pannini_to_xyz(const V360Context *s,
2536 int i, int j, int width, int height,
2539 const float uf = ((2.f * i + 1.f) / width - 1.f);
2540 const float vf = ((2.f * j + 1.f) / height - 1.f);
2542 const float d = s->h_fov;
2543 const float k = uf * uf / ((d + 1.f) * (d + 1.f));
2544 const float dscr = k * k * d * d - (k + 1.f) * (k * d * d - 1.f);
2545 const float clon = (-k * d + sqrtf(dscr)) / (k + 1.f);
2546 const float S = (d + 1.f) / (d + clon);
2547 const float lon = -(M_PI + atan2f(uf, S * clon));
2548 const float lat = -atan2f(vf, S);
2550 vec[0] = sinf(lon) * cosf(lat);
2552 vec[2] = cosf(lon) * cosf(lat);
2554 normalize_vector(vec);
2560 * Prepare data for processing cylindrical output format.
2562 * @param ctx filter context
2564 * @return error code
2566 static int prepare_cylindrical_out(AVFilterContext *ctx)
2568 V360Context *s = ctx->priv;
2570 s->flat_range[0] = M_PI * s->h_fov / 360.f;
2571 s->flat_range[1] = tanf(0.5f * s->v_fov * M_PI / 180.f);
2577 * Calculate 3D coordinates on sphere for corresponding frame position in cylindrical format.
2579 * @param s filter private context
2580 * @param i horizontal position on frame [0, width)
2581 * @param j vertical position on frame [0, height)
2582 * @param width frame width
2583 * @param height frame height
2584 * @param vec coordinates on sphere
2586 static int cylindrical_to_xyz(const V360Context *s,
2587 int i, int j, int width, int height,
2590 const float uf = s->flat_range[0] * ((2.f * i + 1.f) / width - 1.f);
2591 const float vf = s->flat_range[1] * ((2.f * j + 1.f) / height - 1.f);
2593 const float phi = uf;
2594 const float theta = atanf(vf);
2596 const float sin_phi = sinf(phi);
2597 const float cos_phi = cosf(phi);
2598 const float sin_theta = sinf(theta);
2599 const float cos_theta = cosf(theta);
2601 vec[0] = cos_theta * sin_phi;
2602 vec[1] = -sin_theta;
2603 vec[2] = -cos_theta * cos_phi;
2605 normalize_vector(vec);
2611 * Prepare data for processing cylindrical input format.
2613 * @param ctx filter context
2615 * @return error code
2617 static int prepare_cylindrical_in(AVFilterContext *ctx)
2619 V360Context *s = ctx->priv;
2621 s->iflat_range[0] = M_PI * s->ih_fov / 360.f;
2622 s->iflat_range[1] = tanf(0.5f * s->iv_fov * M_PI / 180.f);
2628 * Calculate frame position in cylindrical format for corresponding 3D coordinates on sphere.
2630 * @param s filter private context
2631 * @param vec coordinates on sphere
2632 * @param width frame width
2633 * @param height frame height
2634 * @param us horizontal coordinates for interpolation window
2635 * @param vs vertical coordinates for interpolation window
2636 * @param du horizontal relative coordinate
2637 * @param dv vertical relative coordinate
2639 static int xyz_to_cylindrical(const V360Context *s,
2640 const float *vec, int width, int height,
2641 int16_t us[4][4], int16_t vs[4][4], float *du, float *dv)
2643 const float phi = atan2f(vec[0], -vec[2]) * s->input_mirror_modifier[0] / s->iflat_range[0];
2644 const float theta = asinf(-vec[1]) * s->input_mirror_modifier[1];
2646 const float uf = (phi + 1.f) * (width - 1) / 2.f;
2647 const float vf = (tanf(theta) / s->iflat_range[1] + 1.f) * height / 2.f;
2649 const int ui = floorf(uf);
2650 const int vi = floorf(vf);
2652 const int visible = vi >= 0 && vi < height && ui >= 0 && ui < width &&
2653 theta <= M_PI * s->iv_fov / 180.f &&
2654 theta >= -M_PI * s->iv_fov / 180.f;
2659 for (int i = 0; i < 4; i++) {
2660 for (int j = 0; j < 4; j++) {
2661 us[i][j] = visible ? av_clip(ui + j - 1, 0, width - 1) : 0;
2662 vs[i][j] = visible ? av_clip(vi + i - 1, 0, height - 1) : 0;
2670 * Calculate 3D coordinates on sphere for corresponding frame position in perspective format.
2672 * @param s filter private context
2673 * @param i horizontal position on frame [0, width)
2674 * @param j vertical position on frame [0, height)
2675 * @param width frame width
2676 * @param height frame height
2677 * @param vec coordinates on sphere
2679 static int perspective_to_xyz(const V360Context *s,
2680 int i, int j, int width, int height,
2683 const float uf = ((2.f * i + 1.f) / width - 1.f);
2684 const float vf = ((2.f * j + 1.f) / height - 1.f);
2685 const float rh = hypotf(uf, vf);
2686 const float sinzz = 1.f - rh * rh;
2687 const float h = 1.f + s->v_fov;
2688 const float sinz = (h - sqrtf(sinzz)) / (h / rh + rh / h);
2689 const float sinz2 = sinz * sinz;
2692 const float cosz = sqrtf(1.f - sinz2);
2694 const float theta = asinf(cosz);
2695 const float phi = atan2f(uf, vf);
2697 const float sin_phi = sinf(phi);
2698 const float cos_phi = cosf(phi);
2699 const float sin_theta = sinf(theta);
2700 const float cos_theta = cosf(theta);
2702 vec[0] = cos_theta * sin_phi;
2704 vec[2] = -cos_theta * cos_phi;
2712 normalize_vector(vec);
2717 * Calculate 3D coordinates on sphere for corresponding frame position in tetrahedron format.
2719 * @param s filter private context
2720 * @param i horizontal position on frame [0, width)
2721 * @param j vertical position on frame [0, height)
2722 * @param width frame width
2723 * @param height frame height
2724 * @param vec coordinates on sphere
2726 static int tetrahedron_to_xyz(const V360Context *s,
2727 int i, int j, int width, int height,
2730 const float uf = (float)i / width;
2731 const float vf = (float)j / height;
2733 vec[0] = uf < 0.5f ? uf * 4.f - 1.f : 3.f - uf * 4.f;
2734 vec[1] = 1.f - vf * 2.f;
2735 vec[2] = 2.f * fabsf(1.f - fabsf(1.f - uf * 2.f + vf)) - 1.f;
2737 normalize_vector(vec);
2743 * Calculate frame position in tetrahedron format for corresponding 3D coordinates on sphere.
2745 * @param s filter private context
2746 * @param vec coordinates on sphere
2747 * @param width frame width
2748 * @param height frame height
2749 * @param us horizontal coordinates for interpolation window
2750 * @param vs vertical coordinates for interpolation window
2751 * @param du horizontal relative coordinate
2752 * @param dv vertical relative coordinate
2754 static int xyz_to_tetrahedron(const V360Context *s,
2755 const float *vec, int width, int height,
2756 int16_t us[4][4], int16_t vs[4][4], float *du, float *dv)
2758 const float d0 = vec[0] * 1.f + vec[1] * 1.f + vec[2] *-1.f;
2759 const float d1 = vec[0] *-1.f + vec[1] *-1.f + vec[2] *-1.f;
2760 const float d2 = vec[0] * 1.f + vec[1] *-1.f + vec[2] * 1.f;
2761 const float d3 = vec[0] *-1.f + vec[1] * 1.f + vec[2] * 1.f;
2762 const float d = FFMAX(d0, FFMAX3(d1, d2, d3));
2764 float uf, vf, x, y, z;
2771 vf = 0.5f - y * 0.5f * s->input_mirror_modifier[1];
2773 if ((x + y >= 0.f && y + z >= 0.f && -z - x <= 0.f) ||
2774 (x + y <= 0.f && -y + z >= 0.f && z - x >= 0.f)) {
2775 uf = 0.25f * x * s->input_mirror_modifier[0] + 0.25f;
2777 uf = 0.75f - 0.25f * x * s->input_mirror_modifier[0];
2789 for (int i = 0; i < 4; i++) {
2790 for (int j = 0; j < 4; j++) {
2791 us[i][j] = reflectx(ui + j - 1, vi + i - 1, width, height);
2792 vs[i][j] = reflecty(vi + i - 1, height);
2800 * Calculate 3D coordinates on sphere for corresponding frame position in dual fisheye format.
2802 * @param s filter private context
2803 * @param i horizontal position on frame [0, width)
2804 * @param j vertical position on frame [0, height)
2805 * @param width frame width
2806 * @param height frame height
2807 * @param vec coordinates on sphere
2809 static int dfisheye_to_xyz(const V360Context *s,
2810 int i, int j, int width, int height,
2813 const float scale = 1.f + s->out_pad;
2815 const float ew = width / 2.f;
2816 const float eh = height;
2818 const int ei = i >= ew ? i - ew : i;
2819 const float m = i >= ew ? -1.f : 1.f;
2821 const float uf = ((2.f * ei) / ew - 1.f) * scale;
2822 const float vf = ((2.f * j + 1.f) / eh - 1.f) * scale;
2824 const float h = hypotf(uf, vf);
2825 const float lh = h > 0.f ? h : 1.f;
2826 const float theta = m * M_PI_2 * (1.f - h);
2828 const float sin_theta = sinf(theta);
2829 const float cos_theta = cosf(theta);
2831 vec[0] = cos_theta * m * -uf / lh;
2832 vec[1] = cos_theta * -vf / lh;
2835 normalize_vector(vec);
2841 * Calculate frame position in dual fisheye format for corresponding 3D coordinates on sphere.
2843 * @param s filter private context
2844 * @param vec coordinates on sphere
2845 * @param width frame width
2846 * @param height frame height
2847 * @param us horizontal coordinates for interpolation window
2848 * @param vs vertical coordinates for interpolation window
2849 * @param du horizontal relative coordinate
2850 * @param dv vertical relative coordinate
2852 static int xyz_to_dfisheye(const V360Context *s,
2853 const float *vec, int width, int height,
2854 int16_t us[4][4], int16_t vs[4][4], float *du, float *dv)
2856 const float scale = 1.f - s->in_pad;
2858 const float ew = width / 2.f;
2859 const float eh = height;
2861 const float h = hypotf(vec[0], vec[1]);
2862 const float lh = h > 0.f ? h : 1.f;
2863 const float theta = acosf(fabsf(vec[2])) / M_PI;
2865 float uf = (theta * (-vec[0] / lh) * s->input_mirror_modifier[0] * scale + 0.5f) * ew;
2866 float vf = (theta * (-vec[1] / lh) * s->input_mirror_modifier[1] * scale + 0.5f) * eh;
2871 if (vec[2] >= 0.f) {
2874 u_shift = ceilf(ew);
2884 for (int i = 0; i < 4; i++) {
2885 for (int j = 0; j < 4; j++) {
2886 us[i][j] = av_clip(u_shift + ui + j - 1, 0, width - 1);
2887 vs[i][j] = av_clip( vi + i - 1, 0, height - 1);
2895 * Calculate 3D coordinates on sphere for corresponding frame position in barrel facebook's format.
2897 * @param s filter private context
2898 * @param i horizontal position on frame [0, width)
2899 * @param j vertical position on frame [0, height)
2900 * @param width frame width
2901 * @param height frame height
2902 * @param vec coordinates on sphere
2904 static int barrel_to_xyz(const V360Context *s,
2905 int i, int j, int width, int height,
2908 const float scale = 0.99f;
2909 float l_x, l_y, l_z;
2911 if (i < 4 * width / 5) {
2912 const float theta_range = M_PI_4;
2914 const int ew = 4 * width / 5;
2915 const int eh = height;
2917 const float phi = ((2.f * i) / ew - 1.f) * M_PI / scale;
2918 const float theta = ((2.f * j) / eh - 1.f) * theta_range / scale;
2920 const float sin_phi = sinf(phi);
2921 const float cos_phi = cosf(phi);
2922 const float sin_theta = sinf(theta);
2923 const float cos_theta = cosf(theta);
2925 l_x = cos_theta * sin_phi;
2927 l_z = -cos_theta * cos_phi;
2929 const int ew = width / 5;
2930 const int eh = height / 2;
2935 uf = 2.f * (i - 4 * ew) / ew - 1.f;
2936 vf = 2.f * (j ) / eh - 1.f;
2945 uf = 2.f * (i - 4 * ew) / ew - 1.f;
2946 vf = 2.f * (j - eh) / eh - 1.f;
2961 normalize_vector(vec);
2967 * Calculate frame position in barrel facebook's format for corresponding 3D coordinates on sphere.
2969 * @param s filter private context
2970 * @param vec coordinates on sphere
2971 * @param width frame width
2972 * @param height frame height
2973 * @param us horizontal coordinates for interpolation window
2974 * @param vs vertical coordinates for interpolation window
2975 * @param du horizontal relative coordinate
2976 * @param dv vertical relative coordinate
2978 static int xyz_to_barrel(const V360Context *s,
2979 const float *vec, int width, int height,
2980 int16_t us[4][4], int16_t vs[4][4], float *du, float *dv)
2982 const float scale = 0.99f;
2984 const float phi = atan2f(vec[0], -vec[2]) * s->input_mirror_modifier[0];
2985 const float theta = asinf(-vec[1]) * s->input_mirror_modifier[1];
2986 const float theta_range = M_PI_4;
2989 int u_shift, v_shift;
2993 if (theta > -theta_range && theta < theta_range) {
2997 u_shift = s->ih_flip ? width / 5 : 0;
3000 uf = (phi / M_PI * scale + 1.f) * ew / 2.f;
3001 vf = (theta / theta_range * scale + 1.f) * eh / 2.f;
3006 u_shift = s->ih_flip ? 0 : 4 * ew;
3008 if (theta < 0.f) { // UP
3009 uf = vec[0] / vec[1];
3010 vf = -vec[2] / vec[1];
3013 uf = -vec[0] / vec[1];
3014 vf = -vec[2] / vec[1];
3018 uf *= s->input_mirror_modifier[0] * s->input_mirror_modifier[1];
3019 vf *= s->input_mirror_modifier[1];
3021 uf = 0.5f * ew * (uf * scale + 1.f);
3022 vf = 0.5f * eh * (vf * scale + 1.f);
3031 for (int i = 0; i < 4; i++) {
3032 for (int j = 0; j < 4; j++) {
3033 us[i][j] = u_shift + av_clip(ui + j - 1, 0, ew - 1);
3034 vs[i][j] = v_shift + av_clip(vi + i - 1, 0, eh - 1);
3042 * Calculate frame position in barrel split facebook's format for corresponding 3D coordinates on sphere.
3044 * @param s filter private context
3045 * @param vec coordinates on sphere
3046 * @param width frame width
3047 * @param height frame height
3048 * @param us horizontal coordinates for interpolation window
3049 * @param vs vertical coordinates for interpolation window
3050 * @param du horizontal relative coordinate
3051 * @param dv vertical relative coordinate
3053 static int xyz_to_barrelsplit(const V360Context *s,
3054 const float *vec, int width, int height,
3055 int16_t us[4][4], int16_t vs[4][4], float *du, float *dv)
3057 const float phi = atan2f(vec[0], -vec[2]) * s->input_mirror_modifier[0];
3058 const float theta = asinf(-vec[1]) * s->input_mirror_modifier[1];
3060 const float theta_range = M_PI_4;
3063 int u_shift, v_shift;
3067 if (theta >= -theta_range && theta <= theta_range) {
3068 const float scalew = s->fin_pad > 0 ? 1.f - s->fin_pad / (width * 2.f / 3.f) : 1.f - s->in_pad;
3069 const float scaleh = s->fin_pad > 0 ? 1.f - s->fin_pad / (height / 2.f) : 1.f - s->in_pad;
3074 u_shift = s->ih_flip ? width / 3 : 0;
3075 v_shift = phi >= M_PI_2 || phi < -M_PI_2 ? eh : 0;
3077 uf = fmodf(phi, M_PI_2) / M_PI_2;
3078 vf = theta / M_PI_4;
3081 uf = uf >= 0.f ? fmodf(uf - 1.f, 1.f) : fmodf(uf + 1.f, 1.f);
3083 uf = (uf * scalew + 1.f) * width / 3.f;
3084 vf = (vf * scaleh + 1.f) * height / 4.f;
3086 const float scalew = s->fin_pad > 0 ? 1.f - s->fin_pad / (width / 3.f) : 1.f - s->in_pad;
3087 const float scaleh = s->fin_pad > 0 ? 1.f - s->fin_pad / (height / 4.f) : 1.f - s->in_pad;
3093 u_shift = s->ih_flip ? 0 : 2 * ew;
3095 if (theta <= 0.f && theta >= -M_PI_2 &&
3096 phi <= M_PI_2 && phi >= -M_PI_2) {
3097 uf = vec[0] / vec[1];
3098 vf = -vec[2] / vec[1];
3101 } else if (theta >= 0.f && theta <= M_PI_2 &&
3102 phi <= M_PI_2 && phi >= -M_PI_2) {
3103 uf = -vec[0] / vec[1];
3104 vf = -vec[2] / vec[1];
3105 v_shift = height * 0.25f;
3106 } else if (theta <= 0.f && theta >= -M_PI_2) {
3107 uf = -vec[0] / vec[1];
3108 vf = vec[2] / vec[1];
3109 v_shift = height * 0.5f;
3112 uf = vec[0] / vec[1];
3113 vf = vec[2] / vec[1];
3114 v_shift = height * 0.75f;
3117 uf *= s->input_mirror_modifier[0] * s->input_mirror_modifier[1];
3118 vf *= s->input_mirror_modifier[1];
3120 uf = 0.5f * width / 3.f * (uf * scalew + 1.f);
3121 vf = height * 0.25f * (vf * scaleh + 1.f) + v_offset;
3130 for (int i = 0; i < 4; i++) {
3131 for (int j = 0; j < 4; j++) {
3132 us[i][j] = u_shift + av_clip(ui + j - 1, 0, ew - 1);
3133 vs[i][j] = v_shift + av_clip(vi + i - 1, 0, eh - 1);
3141 * Calculate 3D coordinates on sphere for corresponding frame position in barrel split facebook's format.
3143 * @param s filter private context
3144 * @param i horizontal position on frame [0, width)
3145 * @param j vertical position on frame [0, height)
3146 * @param width frame width
3147 * @param height frame height
3148 * @param vec coordinates on sphere
3150 static int barrelsplit_to_xyz(const V360Context *s,
3151 int i, int j, int width, int height,
3154 const float x = (i + 0.5f) / width;
3155 const float y = (j + 0.5f) / height;
3156 float l_x, l_y, l_z;
3158 if (x < 2.f / 3.f) {
3159 const float scalew = s->fout_pad > 0 ? 1.f - s->fout_pad / (width * 2.f / 3.f) : 1.f - s->out_pad;
3160 const float scaleh = s->fout_pad > 0 ? 1.f - s->fout_pad / (height / 2.f) : 1.f - s->out_pad;
3162 const float back = floorf(y * 2.f);
3164 const float phi = ((3.f / 2.f * x - 0.5f) / scalew - back + 1.f) * M_PI;
3165 const float theta = (y - 0.25f - 0.5f * back) / scaleh * M_PI;
3167 const float sin_phi = sinf(phi);
3168 const float cos_phi = cosf(phi);
3169 const float sin_theta = sinf(theta);
3170 const float cos_theta = cosf(theta);
3172 l_x = -cos_theta * sin_phi;
3174 l_z = cos_theta * cos_phi;
3176 const float scalew = s->fout_pad > 0 ? 1.f - s->fout_pad / (width / 3.f) : 1.f - s->out_pad;
3177 const float scaleh = s->fout_pad > 0 ? 1.f - s->fout_pad / (height / 4.f) : 1.f - s->out_pad;
3179 const int face = floorf(y * 4.f);
3190 l_x = (0.5f - uf) / scalew;
3192 l_z = (-0.5f + vf) / scaleh;
3197 vf = 1.f - (vf - 0.5f);
3199 l_x = (0.5f - uf) / scalew;
3201 l_z = (0.5f - vf) / scaleh;
3204 vf = y * 2.f - 0.5f;
3205 vf = 1.f - (1.f - vf);
3207 l_x = (0.5f - uf) / scalew;
3209 l_z = (-0.5f + vf) / scaleh;
3212 vf = y * 2.f - 1.5f;
3214 l_x = (0.5f - uf) / scalew;
3216 l_z = (0.5f - vf) / scaleh;
3225 normalize_vector(vec);
3230 static void multiply_matrix(float c[3][3], const float a[3][3], const float b[3][3])
3232 for (int i = 0; i < 3; i++) {
3233 for (int j = 0; j < 3; j++) {
3236 for (int k = 0; k < 3; k++)
3237 sum += a[i][k] * b[k][j];
3245 * Calculate rotation matrix for yaw/pitch/roll angles.
3247 static inline void calculate_rotation_matrix(float yaw, float pitch, float roll,
3248 float rot_mat[3][3],
3249 const int rotation_order[3])
3251 const float yaw_rad = yaw * M_PI / 180.f;
3252 const float pitch_rad = pitch * M_PI / 180.f;
3253 const float roll_rad = roll * M_PI / 180.f;
3255 const float sin_yaw = sinf(-yaw_rad);
3256 const float cos_yaw = cosf(-yaw_rad);
3257 const float sin_pitch = sinf(pitch_rad);
3258 const float cos_pitch = cosf(pitch_rad);
3259 const float sin_roll = sinf(roll_rad);
3260 const float cos_roll = cosf(roll_rad);
3265 m[0][0][0] = cos_yaw; m[0][0][1] = 0; m[0][0][2] = sin_yaw;
3266 m[0][1][0] = 0; m[0][1][1] = 1; m[0][1][2] = 0;
3267 m[0][2][0] = -sin_yaw; m[0][2][1] = 0; m[0][2][2] = cos_yaw;
3269 m[1][0][0] = 1; m[1][0][1] = 0; m[1][0][2] = 0;
3270 m[1][1][0] = 0; m[1][1][1] = cos_pitch; m[1][1][2] = -sin_pitch;
3271 m[1][2][0] = 0; m[1][2][1] = sin_pitch; m[1][2][2] = cos_pitch;
3273 m[2][0][0] = cos_roll; m[2][0][1] = -sin_roll; m[2][0][2] = 0;
3274 m[2][1][0] = sin_roll; m[2][1][1] = cos_roll; m[2][1][2] = 0;
3275 m[2][2][0] = 0; m[2][2][1] = 0; m[2][2][2] = 1;
3277 multiply_matrix(temp, m[rotation_order[0]], m[rotation_order[1]]);
3278 multiply_matrix(rot_mat, temp, m[rotation_order[2]]);
3282 * Rotate vector with given rotation matrix.
3284 * @param rot_mat rotation matrix
3287 static inline void rotate(const float rot_mat[3][3],
3290 const float x_tmp = vec[0] * rot_mat[0][0] + vec[1] * rot_mat[0][1] + vec[2] * rot_mat[0][2];
3291 const float y_tmp = vec[0] * rot_mat[1][0] + vec[1] * rot_mat[1][1] + vec[2] * rot_mat[1][2];
3292 const float z_tmp = vec[0] * rot_mat[2][0] + vec[1] * rot_mat[2][1] + vec[2] * rot_mat[2][2];
3299 static inline void set_mirror_modifier(int h_flip, int v_flip, int d_flip,
3302 modifier[0] = h_flip ? -1.f : 1.f;
3303 modifier[1] = v_flip ? -1.f : 1.f;
3304 modifier[2] = d_flip ? -1.f : 1.f;
3307 static inline void mirror(const float *modifier, float *vec)
3309 vec[0] *= modifier[0];
3310 vec[1] *= modifier[1];
3311 vec[2] *= modifier[2];
3314 static int allocate_plane(V360Context *s, int sizeof_uv, int sizeof_ker, int sizeof_mask, int p)
3317 s->u[p] = av_calloc(s->uv_linesize[p] * s->pr_height[p], sizeof_uv);
3319 s->v[p] = av_calloc(s->uv_linesize[p] * s->pr_height[p], sizeof_uv);
3320 if (!s->u[p] || !s->v[p])
3321 return AVERROR(ENOMEM);
3324 s->ker[p] = av_calloc(s->uv_linesize[p] * s->pr_height[p], sizeof_ker);
3326 return AVERROR(ENOMEM);
3329 if (sizeof_mask && !p) {
3331 s->mask = av_calloc(s->pr_width[p] * s->pr_height[p], sizeof_mask);
3333 return AVERROR(ENOMEM);
3339 static void fov_from_dfov(int format, float d_fov, float w, float h, float *h_fov, float *v_fov)
3344 const float d = 0.5f * hypotf(w, h);
3346 *h_fov = d / h * d_fov;
3347 *v_fov = d / w * d_fov;
3353 const float da = tanf(0.5 * FFMIN(d_fov, 359.f) * M_PI / 180.f);
3354 const float d = hypotf(w, h);
3356 *h_fov = atan2f(da * w, d) * 360.f / M_PI;
3357 *v_fov = atan2f(da * h, d) * 360.f / M_PI;
3368 static void set_dimensions(int *outw, int *outh, int w, int h, const AVPixFmtDescriptor *desc)
3370 outw[1] = outw[2] = FF_CEIL_RSHIFT(w, desc->log2_chroma_w);
3371 outw[0] = outw[3] = w;
3372 outh[1] = outh[2] = FF_CEIL_RSHIFT(h, desc->log2_chroma_h);
3373 outh[0] = outh[3] = h;
3376 // Calculate remap data
3377 static av_always_inline int v360_slice(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
3379 V360Context *s = ctx->priv;
3381 for (int p = 0; p < s->nb_allocated; p++) {
3382 const int max_value = s->max_value;
3383 const int width = s->pr_width[p];
3384 const int uv_linesize = s->uv_linesize[p];
3385 const int height = s->pr_height[p];
3386 const int in_width = s->inplanewidth[p];
3387 const int in_height = s->inplaneheight[p];
3388 const int slice_start = (height * jobnr ) / nb_jobs;
3389 const int slice_end = (height * (jobnr + 1)) / nb_jobs;
3394 for (int j = slice_start; j < slice_end; j++) {
3395 for (int i = 0; i < width; i++) {
3396 int16_t *u = s->u[p] + (j * uv_linesize + i) * s->elements;
3397 int16_t *v = s->v[p] + (j * uv_linesize + i) * s->elements;
3398 int16_t *ker = s->ker[p] + (j * uv_linesize + i) * s->elements;
3399 uint8_t *mask8 = p ? NULL : s->mask + (j * s->pr_width[0] + i);
3400 uint16_t *mask16 = p ? NULL : (uint16_t *)s->mask + (j * s->pr_width[0] + i);
3401 int in_mask, out_mask;
3403 if (s->out_transpose)
3404 out_mask = s->out_transform(s, j, i, height, width, vec);
3406 out_mask = s->out_transform(s, i, j, width, height, vec);
3407 av_assert1(!isnan(vec[0]) && !isnan(vec[1]) && !isnan(vec[2]));
3408 rotate(s->rot_mat, vec);
3409 av_assert1(!isnan(vec[0]) && !isnan(vec[1]) && !isnan(vec[2]));
3410 normalize_vector(vec);
3411 mirror(s->output_mirror_modifier, vec);
3412 if (s->in_transpose)
3413 in_mask = s->in_transform(s, vec, in_height, in_width, rmap.v, rmap.u, &du, &dv);
3415 in_mask = s->in_transform(s, vec, in_width, in_height, rmap.u, rmap.v, &du, &dv);
3416 av_assert1(!isnan(du) && !isnan(dv));
3417 s->calculate_kernel(du, dv, &rmap, u, v, ker);
3419 if (!p && s->mask) {
3420 if (s->mask_size == 1) {
3421 mask8[0] = 255 * (out_mask & in_mask);
3423 mask16[0] = max_value * (out_mask & in_mask);
3433 static int config_output(AVFilterLink *outlink)
3435 AVFilterContext *ctx = outlink->src;
3436 AVFilterLink *inlink = ctx->inputs[0];
3437 V360Context *s = ctx->priv;
3438 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(inlink->format);
3439 const int depth = desc->comp[0].depth;
3440 const int sizeof_mask = s->mask_size = (depth + 7) >> 3;
3445 int in_offset_h, in_offset_w;
3446 int out_offset_h, out_offset_w;
3448 int (*prepare_out)(AVFilterContext *ctx);
3451 s->max_value = (1 << depth) - 1;
3452 s->input_mirror_modifier[0] = s->ih_flip ? -1.f : 1.f;
3453 s->input_mirror_modifier[1] = s->iv_flip ? -1.f : 1.f;
3455 switch (s->interp) {
3457 s->calculate_kernel = nearest_kernel;
3458 s->remap_slice = depth <= 8 ? remap1_8bit_slice : remap1_16bit_slice;
3460 sizeof_uv = sizeof(int16_t) * s->elements;
3464 s->calculate_kernel = bilinear_kernel;
3465 s->remap_slice = depth <= 8 ? remap2_8bit_slice : remap2_16bit_slice;
3466 s->elements = 2 * 2;
3467 sizeof_uv = sizeof(int16_t) * s->elements;
3468 sizeof_ker = sizeof(int16_t) * s->elements;
3471 s->calculate_kernel = bicubic_kernel;
3472 s->remap_slice = depth <= 8 ? remap4_8bit_slice : remap4_16bit_slice;
3473 s->elements = 4 * 4;
3474 sizeof_uv = sizeof(int16_t) * s->elements;
3475 sizeof_ker = sizeof(int16_t) * s->elements;
3478 s->calculate_kernel = lanczos_kernel;
3479 s->remap_slice = depth <= 8 ? remap4_8bit_slice : remap4_16bit_slice;
3480 s->elements = 4 * 4;
3481 sizeof_uv = sizeof(int16_t) * s->elements;
3482 sizeof_ker = sizeof(int16_t) * s->elements;
3485 s->calculate_kernel = spline16_kernel;
3486 s->remap_slice = depth <= 8 ? remap4_8bit_slice : remap4_16bit_slice;
3487 s->elements = 4 * 4;
3488 sizeof_uv = sizeof(int16_t) * s->elements;
3489 sizeof_ker = sizeof(int16_t) * s->elements;
3492 s->calculate_kernel = gaussian_kernel;
3493 s->remap_slice = depth <= 8 ? remap4_8bit_slice : remap4_16bit_slice;
3494 s->elements = 4 * 4;
3495 sizeof_uv = sizeof(int16_t) * s->elements;
3496 sizeof_ker = sizeof(int16_t) * s->elements;
3502 ff_v360_init(s, depth);
3504 for (int order = 0; order < NB_RORDERS; order++) {
3505 const char c = s->rorder[order];
3509 av_log(ctx, AV_LOG_WARNING,
3510 "Incomplete rorder option. Direction for all 3 rotation orders should be specified. Switching to default rorder.\n");
3511 s->rotation_order[0] = YAW;
3512 s->rotation_order[1] = PITCH;
3513 s->rotation_order[2] = ROLL;
3517 rorder = get_rorder(c);
3519 av_log(ctx, AV_LOG_WARNING,
3520 "Incorrect rotation order symbol '%c' in rorder option. Switching to default rorder.\n", c);
3521 s->rotation_order[0] = YAW;
3522 s->rotation_order[1] = PITCH;
3523 s->rotation_order[2] = ROLL;
3527 s->rotation_order[order] = rorder;
3530 switch (s->in_stereo) {
3534 in_offset_w = in_offset_h = 0;
3552 set_dimensions(s->inplanewidth, s->inplaneheight, w, h, desc);
3553 set_dimensions(s->in_offset_w, s->in_offset_h, in_offset_w, in_offset_h, desc);
3555 s->in_width = s->inplanewidth[0];
3556 s->in_height = s->inplaneheight[0];
3558 if (s->id_fov > 0.f)
3559 fov_from_dfov(s->in, s->id_fov, w, h, &s->ih_fov, &s->iv_fov);
3561 if (s->in_transpose)
3562 FFSWAP(int, s->in_width, s->in_height);
3565 case EQUIRECTANGULAR:
3566 s->in_transform = xyz_to_equirect;
3572 s->in_transform = xyz_to_cube3x2;
3573 err = prepare_cube_in(ctx);
3578 s->in_transform = xyz_to_cube1x6;
3579 err = prepare_cube_in(ctx);
3584 s->in_transform = xyz_to_cube6x1;
3585 err = prepare_cube_in(ctx);
3590 s->in_transform = xyz_to_eac;
3591 err = prepare_eac_in(ctx);
3596 s->in_transform = xyz_to_flat;
3597 err = prepare_flat_in(ctx);
3603 av_log(ctx, AV_LOG_ERROR, "Supplied format is not accepted as input.\n");
3604 return AVERROR(EINVAL);
3606 s->in_transform = xyz_to_dfisheye;
3612 s->in_transform = xyz_to_barrel;
3618 s->in_transform = xyz_to_stereographic;
3619 err = prepare_stereographic_in(ctx);
3624 s->in_transform = xyz_to_mercator;
3630 s->in_transform = xyz_to_ball;
3636 s->in_transform = xyz_to_hammer;
3642 s->in_transform = xyz_to_sinusoidal;
3648 s->in_transform = xyz_to_fisheye;
3649 err = prepare_fisheye_in(ctx);
3654 s->in_transform = xyz_to_cylindrical;
3655 err = prepare_cylindrical_in(ctx);
3660 s->in_transform = xyz_to_tetrahedron;
3666 s->in_transform = xyz_to_barrelsplit;
3672 av_log(ctx, AV_LOG_ERROR, "Specified input format is not handled.\n");
3681 case EQUIRECTANGULAR:
3682 s->out_transform = equirect_to_xyz;
3688 s->out_transform = cube3x2_to_xyz;
3689 prepare_out = prepare_cube_out;
3690 w = lrintf(wf / 4.f * 3.f);
3694 s->out_transform = cube1x6_to_xyz;
3695 prepare_out = prepare_cube_out;
3696 w = lrintf(wf / 4.f);
3697 h = lrintf(hf * 3.f);
3700 s->out_transform = cube6x1_to_xyz;
3701 prepare_out = prepare_cube_out;
3702 w = lrintf(wf / 2.f * 3.f);
3703 h = lrintf(hf / 2.f);
3706 s->out_transform = eac_to_xyz;
3707 prepare_out = prepare_eac_out;
3709 h = lrintf(hf / 8.f * 9.f);
3712 s->out_transform = flat_to_xyz;
3713 prepare_out = prepare_flat_out;
3718 s->out_transform = dfisheye_to_xyz;
3724 s->out_transform = barrel_to_xyz;
3726 w = lrintf(wf / 4.f * 5.f);
3730 s->out_transform = stereographic_to_xyz;
3731 prepare_out = prepare_stereographic_out;
3733 h = lrintf(hf * 2.f);
3736 s->out_transform = mercator_to_xyz;
3739 h = lrintf(hf * 2.f);
3742 s->out_transform = ball_to_xyz;
3745 h = lrintf(hf * 2.f);
3748 s->out_transform = hammer_to_xyz;
3754 s->out_transform = sinusoidal_to_xyz;
3760 s->out_transform = fisheye_to_xyz;
3761 prepare_out = prepare_fisheye_out;
3762 w = lrintf(wf * 0.5f);
3766 s->out_transform = pannini_to_xyz;
3772 s->out_transform = cylindrical_to_xyz;
3773 prepare_out = prepare_cylindrical_out;
3775 h = lrintf(hf * 0.5f);
3778 s->out_transform = perspective_to_xyz;
3780 w = lrintf(wf / 2.f);
3784 s->out_transform = tetrahedron_to_xyz;
3790 s->out_transform = barrelsplit_to_xyz;
3792 w = lrintf(wf / 4.f * 3.f);
3796 av_log(ctx, AV_LOG_ERROR, "Specified output format is not handled.\n");
3800 // Override resolution with user values if specified
3801 if (s->width > 0 && s->height <= 0 && s->h_fov > 0.f && s->v_fov > 0.f &&
3802 s->out == FLAT && s->d_fov == 0.f) {
3804 h = w / tanf(s->h_fov * M_PI / 360.f) * tanf(s->v_fov * M_PI / 360.f);
3805 } else if (s->width <= 0 && s->height > 0 && s->h_fov > 0.f && s->v_fov > 0.f &&
3806 s->out == FLAT && s->d_fov == 0.f) {
3808 w = h / tanf(s->v_fov * M_PI / 360.f) * tanf(s->h_fov * M_PI / 360.f);
3809 } else if (s->width > 0 && s->height > 0) {
3812 } else if (s->width > 0 || s->height > 0) {
3813 av_log(ctx, AV_LOG_ERROR, "Both width and height values should be specified.\n");
3814 return AVERROR(EINVAL);
3816 if (s->out_transpose)
3819 if (s->in_transpose)
3827 fov_from_dfov(s->out, s->d_fov, w, h, &s->h_fov, &s->v_fov);
3830 err = prepare_out(ctx);
3835 set_dimensions(s->pr_width, s->pr_height, w, h, desc);
3837 s->out_width = s->pr_width[0];
3838 s->out_height = s->pr_height[0];
3840 if (s->out_transpose)
3841 FFSWAP(int, s->out_width, s->out_height);
3843 switch (s->out_stereo) {
3845 out_offset_w = out_offset_h = 0;
3861 set_dimensions(s->out_offset_w, s->out_offset_h, out_offset_w, out_offset_h, desc);
3862 set_dimensions(s->planewidth, s->planeheight, w, h, desc);
3864 for (int i = 0; i < 4; i++)
3865 s->uv_linesize[i] = FFALIGN(s->pr_width[i], 8);
3870 s->nb_planes = av_pix_fmt_count_planes(inlink->format);
3871 have_alpha = !!(desc->flags & AV_PIX_FMT_FLAG_ALPHA);
3873 if (desc->log2_chroma_h == desc->log2_chroma_w && desc->log2_chroma_h == 0) {
3874 s->nb_allocated = 1;
3875 s->map[0] = s->map[1] = s->map[2] = s->map[3] = 0;
3877 s->nb_allocated = 2;
3878 s->map[0] = s->map[3] = 0;
3879 s->map[1] = s->map[2] = 1;
3882 for (int i = 0; i < s->nb_allocated; i++)
3883 allocate_plane(s, sizeof_uv, sizeof_ker, sizeof_mask * have_alpha * s->alpha, i);
3885 calculate_rotation_matrix(s->yaw, s->pitch, s->roll, s->rot_mat, s->rotation_order);
3886 set_mirror_modifier(s->h_flip, s->v_flip, s->d_flip, s->output_mirror_modifier);
3888 ctx->internal->execute(ctx, v360_slice, NULL, NULL, FFMIN(outlink->h, ff_filter_get_nb_threads(ctx)));
3893 static int filter_frame(AVFilterLink *inlink, AVFrame *in)
3895 AVFilterContext *ctx = inlink->dst;
3896 AVFilterLink *outlink = ctx->outputs[0];
3897 V360Context *s = ctx->priv;
3901 out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
3904 return AVERROR(ENOMEM);
3906 av_frame_copy_props(out, in);
3911 ctx->internal->execute(ctx, s->remap_slice, &td, NULL, FFMIN(outlink->h, ff_filter_get_nb_threads(ctx)));
3914 return ff_filter_frame(outlink, out);
3917 static int process_command(AVFilterContext *ctx, const char *cmd, const char *args,
3918 char *res, int res_len, int flags)
3922 ret = ff_filter_process_command(ctx, cmd, args, res, res_len, flags);
3926 return config_output(ctx->outputs[0]);
3929 static av_cold void uninit(AVFilterContext *ctx)
3931 V360Context *s = ctx->priv;
3933 for (int p = 0; p < s->nb_allocated; p++) {
3936 av_freep(&s->ker[p]);
3941 static const AVFilterPad inputs[] = {
3944 .type = AVMEDIA_TYPE_VIDEO,
3945 .filter_frame = filter_frame,
3950 static const AVFilterPad outputs[] = {
3953 .type = AVMEDIA_TYPE_VIDEO,
3954 .config_props = config_output,
3959 AVFilter ff_vf_v360 = {
3961 .description = NULL_IF_CONFIG_SMALL("Convert 360 projection of video."),
3962 .priv_size = sizeof(V360Context),
3964 .query_formats = query_formats,
3967 .priv_class = &v360_class,
3968 .flags = AVFILTER_FLAG_SLICE_THREADS,
3969 .process_command = process_command,