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
56 static const AVOption v360_options[] = {
57 { "input", "set input projection", OFFSET(in), AV_OPT_TYPE_INT, {.i64=EQUIRECTANGULAR}, 0, NB_PROJECTIONS-1, FLAGS, "in" },
58 { "e", "equirectangular", 0, AV_OPT_TYPE_CONST, {.i64=EQUIRECTANGULAR}, 0, 0, FLAGS, "in" },
59 { "equirect", "equirectangular", 0, AV_OPT_TYPE_CONST, {.i64=EQUIRECTANGULAR}, 0, 0, FLAGS, "in" },
60 { "c3x2", "cubemap 3x2", 0, AV_OPT_TYPE_CONST, {.i64=CUBEMAP_3_2}, 0, 0, FLAGS, "in" },
61 { "c6x1", "cubemap 6x1", 0, AV_OPT_TYPE_CONST, {.i64=CUBEMAP_6_1}, 0, 0, FLAGS, "in" },
62 { "eac", "equi-angular cubemap", 0, AV_OPT_TYPE_CONST, {.i64=EQUIANGULAR}, 0, 0, FLAGS, "in" },
63 { "dfisheye", "dual fisheye", 0, AV_OPT_TYPE_CONST, {.i64=DUAL_FISHEYE}, 0, 0, FLAGS, "in" },
64 { "flat", "regular video", 0, AV_OPT_TYPE_CONST, {.i64=FLAT}, 0, 0, FLAGS, "in" },
65 {"rectilinear", "regular video", 0, AV_OPT_TYPE_CONST, {.i64=FLAT}, 0, 0, FLAGS, "in" },
66 { "gnomonic", "regular video", 0, AV_OPT_TYPE_CONST, {.i64=FLAT}, 0, 0, FLAGS, "in" },
67 { "barrel", "barrel facebook's 360 format", 0, AV_OPT_TYPE_CONST, {.i64=BARREL}, 0, 0, FLAGS, "in" },
68 { "fb", "barrel facebook's 360 format", 0, AV_OPT_TYPE_CONST, {.i64=BARREL}, 0, 0, FLAGS, "in" },
69 { "c1x6", "cubemap 1x6", 0, AV_OPT_TYPE_CONST, {.i64=CUBEMAP_1_6}, 0, 0, FLAGS, "in" },
70 { "sg", "stereographic", 0, AV_OPT_TYPE_CONST, {.i64=STEREOGRAPHIC}, 0, 0, FLAGS, "in" },
71 { "mercator", "mercator", 0, AV_OPT_TYPE_CONST, {.i64=MERCATOR}, 0, 0, FLAGS, "in" },
72 { "ball", "ball", 0, AV_OPT_TYPE_CONST, {.i64=BALL}, 0, 0, FLAGS, "in" },
73 { "hammer", "hammer", 0, AV_OPT_TYPE_CONST, {.i64=HAMMER}, 0, 0, FLAGS, "in" },
74 {"sinusoidal", "sinusoidal", 0, AV_OPT_TYPE_CONST, {.i64=SINUSOIDAL}, 0, 0, FLAGS, "in" },
75 { "fisheye", "fisheye", 0, AV_OPT_TYPE_CONST, {.i64=FISHEYE}, 0, 0, FLAGS, "in" },
76 {"cylindrical", "cylindrical", 0, AV_OPT_TYPE_CONST, {.i64=CYLINDRICAL}, 0, 0, FLAGS, "in" },
77 {"tetrahedron", "tetrahedron", 0, AV_OPT_TYPE_CONST, {.i64=TETRAHEDRON}, 0, 0, FLAGS, "in" },
78 { "output", "set output projection", OFFSET(out), AV_OPT_TYPE_INT, {.i64=CUBEMAP_3_2}, 0, NB_PROJECTIONS-1, FLAGS, "out" },
79 { "e", "equirectangular", 0, AV_OPT_TYPE_CONST, {.i64=EQUIRECTANGULAR}, 0, 0, FLAGS, "out" },
80 { "equirect", "equirectangular", 0, AV_OPT_TYPE_CONST, {.i64=EQUIRECTANGULAR}, 0, 0, FLAGS, "out" },
81 { "c3x2", "cubemap 3x2", 0, AV_OPT_TYPE_CONST, {.i64=CUBEMAP_3_2}, 0, 0, FLAGS, "out" },
82 { "c6x1", "cubemap 6x1", 0, AV_OPT_TYPE_CONST, {.i64=CUBEMAP_6_1}, 0, 0, FLAGS, "out" },
83 { "eac", "equi-angular cubemap", 0, AV_OPT_TYPE_CONST, {.i64=EQUIANGULAR}, 0, 0, FLAGS, "out" },
84 { "dfisheye", "dual fisheye", 0, AV_OPT_TYPE_CONST, {.i64=DUAL_FISHEYE}, 0, 0, FLAGS, "out" },
85 { "flat", "regular video", 0, AV_OPT_TYPE_CONST, {.i64=FLAT}, 0, 0, FLAGS, "out" },
86 {"rectilinear", "regular video", 0, AV_OPT_TYPE_CONST, {.i64=FLAT}, 0, 0, FLAGS, "out" },
87 { "gnomonic", "regular video", 0, AV_OPT_TYPE_CONST, {.i64=FLAT}, 0, 0, FLAGS, "out" },
88 { "barrel", "barrel facebook's 360 format", 0, AV_OPT_TYPE_CONST, {.i64=BARREL}, 0, 0, FLAGS, "out" },
89 { "fb", "barrel facebook's 360 format", 0, AV_OPT_TYPE_CONST, {.i64=BARREL}, 0, 0, FLAGS, "out" },
90 { "c1x6", "cubemap 1x6", 0, AV_OPT_TYPE_CONST, {.i64=CUBEMAP_1_6}, 0, 0, FLAGS, "out" },
91 { "sg", "stereographic", 0, AV_OPT_TYPE_CONST, {.i64=STEREOGRAPHIC}, 0, 0, FLAGS, "out" },
92 { "mercator", "mercator", 0, AV_OPT_TYPE_CONST, {.i64=MERCATOR}, 0, 0, FLAGS, "out" },
93 { "ball", "ball", 0, AV_OPT_TYPE_CONST, {.i64=BALL}, 0, 0, FLAGS, "out" },
94 { "hammer", "hammer", 0, AV_OPT_TYPE_CONST, {.i64=HAMMER}, 0, 0, FLAGS, "out" },
95 {"sinusoidal", "sinusoidal", 0, AV_OPT_TYPE_CONST, {.i64=SINUSOIDAL}, 0, 0, FLAGS, "out" },
96 { "fisheye", "fisheye", 0, AV_OPT_TYPE_CONST, {.i64=FISHEYE}, 0, 0, FLAGS, "out" },
97 { "pannini", "pannini", 0, AV_OPT_TYPE_CONST, {.i64=PANNINI}, 0, 0, FLAGS, "out" },
98 {"cylindrical", "cylindrical", 0, AV_OPT_TYPE_CONST, {.i64=CYLINDRICAL}, 0, 0, FLAGS, "out" },
99 {"perspective", "perspective", 0, AV_OPT_TYPE_CONST, {.i64=PERSPECTIVE}, 0, 0, FLAGS, "out" },
100 {"tetrahedron", "tetrahedron", 0, AV_OPT_TYPE_CONST, {.i64=TETRAHEDRON}, 0, 0, FLAGS, "out" },
101 { "interp", "set interpolation method", OFFSET(interp), AV_OPT_TYPE_INT, {.i64=BILINEAR}, 0, NB_INTERP_METHODS-1, FLAGS, "interp" },
102 { "near", "nearest neighbour", 0, AV_OPT_TYPE_CONST, {.i64=NEAREST}, 0, 0, FLAGS, "interp" },
103 { "nearest", "nearest neighbour", 0, AV_OPT_TYPE_CONST, {.i64=NEAREST}, 0, 0, FLAGS, "interp" },
104 { "line", "bilinear interpolation", 0, AV_OPT_TYPE_CONST, {.i64=BILINEAR}, 0, 0, FLAGS, "interp" },
105 { "linear", "bilinear interpolation", 0, AV_OPT_TYPE_CONST, {.i64=BILINEAR}, 0, 0, FLAGS, "interp" },
106 { "cube", "bicubic interpolation", 0, AV_OPT_TYPE_CONST, {.i64=BICUBIC}, 0, 0, FLAGS, "interp" },
107 { "cubic", "bicubic interpolation", 0, AV_OPT_TYPE_CONST, {.i64=BICUBIC}, 0, 0, FLAGS, "interp" },
108 { "lanc", "lanczos interpolation", 0, AV_OPT_TYPE_CONST, {.i64=LANCZOS}, 0, 0, FLAGS, "interp" },
109 { "lanczos", "lanczos interpolation", 0, AV_OPT_TYPE_CONST, {.i64=LANCZOS}, 0, 0, FLAGS, "interp" },
110 { "sp16", "spline16 interpolation", 0, AV_OPT_TYPE_CONST, {.i64=SPLINE16}, 0, 0, FLAGS, "interp" },
111 { "spline16", "spline16 interpolation", 0, AV_OPT_TYPE_CONST, {.i64=SPLINE16}, 0, 0, FLAGS, "interp" },
112 { "gauss", "gaussian interpolation", 0, AV_OPT_TYPE_CONST, {.i64=GAUSSIAN}, 0, 0, FLAGS, "interp" },
113 { "gaussian", "gaussian interpolation", 0, AV_OPT_TYPE_CONST, {.i64=GAUSSIAN}, 0, 0, FLAGS, "interp" },
114 { "w", "output width", OFFSET(width), AV_OPT_TYPE_INT, {.i64=0}, 0, INT16_MAX, FLAGS, "w"},
115 { "h", "output height", OFFSET(height), AV_OPT_TYPE_INT, {.i64=0}, 0, INT16_MAX, FLAGS, "h"},
116 { "in_stereo", "input stereo format", OFFSET(in_stereo), AV_OPT_TYPE_INT, {.i64=STEREO_2D}, 0, NB_STEREO_FMTS-1, FLAGS, "stereo" },
117 {"out_stereo", "output stereo format", OFFSET(out_stereo), AV_OPT_TYPE_INT, {.i64=STEREO_2D}, 0, NB_STEREO_FMTS-1, FLAGS, "stereo" },
118 { "2d", "2d mono", 0, AV_OPT_TYPE_CONST, {.i64=STEREO_2D}, 0, 0, FLAGS, "stereo" },
119 { "sbs", "side by side", 0, AV_OPT_TYPE_CONST, {.i64=STEREO_SBS}, 0, 0, FLAGS, "stereo" },
120 { "tb", "top bottom", 0, AV_OPT_TYPE_CONST, {.i64=STEREO_TB}, 0, 0, FLAGS, "stereo" },
121 { "in_forder", "input cubemap face order", OFFSET(in_forder), AV_OPT_TYPE_STRING, {.str="rludfb"}, 0, NB_DIRECTIONS-1, FLAGS, "in_forder"},
122 {"out_forder", "output cubemap face order", OFFSET(out_forder), AV_OPT_TYPE_STRING, {.str="rludfb"}, 0, NB_DIRECTIONS-1, FLAGS, "out_forder"},
123 { "in_frot", "input cubemap face rotation", OFFSET(in_frot), AV_OPT_TYPE_STRING, {.str="000000"}, 0, NB_DIRECTIONS-1, FLAGS, "in_frot"},
124 { "out_frot", "output cubemap face rotation",OFFSET(out_frot), AV_OPT_TYPE_STRING, {.str="000000"}, 0, NB_DIRECTIONS-1, FLAGS, "out_frot"},
125 { "in_pad", "percent input cubemap pads", OFFSET(in_pad), AV_OPT_TYPE_FLOAT, {.dbl=0.f}, 0.f, 1.f, FLAGS, "in_pad"},
126 { "out_pad", "percent output cubemap pads", OFFSET(out_pad), AV_OPT_TYPE_FLOAT, {.dbl=0.f}, 0.f, 1.f, FLAGS, "out_pad"},
127 { "fin_pad", "fixed input cubemap pads", OFFSET(fin_pad), AV_OPT_TYPE_INT, {.i64=0}, 0, 100, FLAGS, "fin_pad"},
128 { "fout_pad", "fixed output cubemap pads", OFFSET(fout_pad), AV_OPT_TYPE_INT, {.i64=0}, 0, 100, FLAGS, "fout_pad"},
129 { "yaw", "yaw rotation", OFFSET(yaw), AV_OPT_TYPE_FLOAT, {.dbl=0.f}, -180.f, 180.f, FLAGS, "yaw"},
130 { "pitch", "pitch rotation", OFFSET(pitch), AV_OPT_TYPE_FLOAT, {.dbl=0.f}, -180.f, 180.f, FLAGS, "pitch"},
131 { "roll", "roll rotation", OFFSET(roll), AV_OPT_TYPE_FLOAT, {.dbl=0.f}, -180.f, 180.f, FLAGS, "roll"},
132 { "rorder", "rotation order", OFFSET(rorder), AV_OPT_TYPE_STRING, {.str="ypr"}, 0, 0, FLAGS, "rorder"},
133 { "h_fov", "horizontal field of view", OFFSET(h_fov), AV_OPT_TYPE_FLOAT, {.dbl=90.f}, 0.00001f, 360.f, FLAGS, "h_fov"},
134 { "v_fov", "vertical field of view", OFFSET(v_fov), AV_OPT_TYPE_FLOAT, {.dbl=45.f}, 0.00001f, 360.f, FLAGS, "v_fov"},
135 { "d_fov", "diagonal field of view", OFFSET(d_fov), AV_OPT_TYPE_FLOAT, {.dbl=0.f}, 0.f, 360.f, FLAGS, "d_fov"},
136 { "h_flip", "flip out video horizontally", OFFSET(h_flip), AV_OPT_TYPE_BOOL, {.i64=0}, 0, 1, FLAGS, "h_flip"},
137 { "v_flip", "flip out video vertically", OFFSET(v_flip), AV_OPT_TYPE_BOOL, {.i64=0}, 0, 1, FLAGS, "v_flip"},
138 { "d_flip", "flip out video indepth", OFFSET(d_flip), AV_OPT_TYPE_BOOL, {.i64=0}, 0, 1, FLAGS, "d_flip"},
139 { "ih_flip", "flip in video horizontally", OFFSET(ih_flip), AV_OPT_TYPE_BOOL, {.i64=0}, 0, 1, FLAGS, "ih_flip"},
140 { "iv_flip", "flip in video vertically", OFFSET(iv_flip), AV_OPT_TYPE_BOOL, {.i64=0}, 0, 1, FLAGS, "iv_flip"},
141 { "in_trans", "transpose video input", OFFSET(in_transpose), AV_OPT_TYPE_BOOL, {.i64=0}, 0, 1, FLAGS, "in_transpose"},
142 { "out_trans", "transpose video output", OFFSET(out_transpose), AV_OPT_TYPE_BOOL, {.i64=0}, 0, 1, FLAGS, "out_transpose"},
143 { "ih_fov", "input horizontal field of view",OFFSET(ih_fov), AV_OPT_TYPE_FLOAT, {.dbl=90.f}, 0.00001f, 360.f, FLAGS, "ih_fov"},
144 { "iv_fov", "input vertical field of view", OFFSET(iv_fov), AV_OPT_TYPE_FLOAT, {.dbl=45.f}, 0.00001f, 360.f, FLAGS, "iv_fov"},
145 { "id_fov", "input diagonal field of view", OFFSET(id_fov), AV_OPT_TYPE_FLOAT, {.dbl=0.f}, 0.f, 360.f, FLAGS, "id_fov"},
146 {"alpha_mask", "build mask in alpha plane", OFFSET(alpha), AV_OPT_TYPE_BOOL, {.i64=0}, 0, 1, FLAGS, "alpha"},
150 AVFILTER_DEFINE_CLASS(v360);
152 static int query_formats(AVFilterContext *ctx)
154 V360Context *s = ctx->priv;
155 static const enum AVPixelFormat pix_fmts[] = {
157 AV_PIX_FMT_YUVA444P, AV_PIX_FMT_YUVA444P9,
158 AV_PIX_FMT_YUVA444P10, AV_PIX_FMT_YUVA444P12,
159 AV_PIX_FMT_YUVA444P16,
162 AV_PIX_FMT_YUVA422P, AV_PIX_FMT_YUVA422P9,
163 AV_PIX_FMT_YUVA422P10, AV_PIX_FMT_YUVA422P12,
164 AV_PIX_FMT_YUVA422P16,
167 AV_PIX_FMT_YUVA420P, AV_PIX_FMT_YUVA420P9,
168 AV_PIX_FMT_YUVA420P10, AV_PIX_FMT_YUVA420P16,
171 AV_PIX_FMT_YUVJ444P, AV_PIX_FMT_YUVJ440P,
172 AV_PIX_FMT_YUVJ422P, AV_PIX_FMT_YUVJ420P,
176 AV_PIX_FMT_YUV444P, AV_PIX_FMT_YUV444P9,
177 AV_PIX_FMT_YUV444P10, AV_PIX_FMT_YUV444P12,
178 AV_PIX_FMT_YUV444P14, AV_PIX_FMT_YUV444P16,
181 AV_PIX_FMT_YUV440P, AV_PIX_FMT_YUV440P10,
182 AV_PIX_FMT_YUV440P12,
185 AV_PIX_FMT_YUV422P, AV_PIX_FMT_YUV422P9,
186 AV_PIX_FMT_YUV422P10, AV_PIX_FMT_YUV422P12,
187 AV_PIX_FMT_YUV422P14, AV_PIX_FMT_YUV422P16,
190 AV_PIX_FMT_YUV420P, AV_PIX_FMT_YUV420P9,
191 AV_PIX_FMT_YUV420P10, AV_PIX_FMT_YUV420P12,
192 AV_PIX_FMT_YUV420P14, AV_PIX_FMT_YUV420P16,
201 AV_PIX_FMT_GBRP, AV_PIX_FMT_GBRP9,
202 AV_PIX_FMT_GBRP10, AV_PIX_FMT_GBRP12,
203 AV_PIX_FMT_GBRP14, AV_PIX_FMT_GBRP16,
206 AV_PIX_FMT_GBRAP, AV_PIX_FMT_GBRAP10,
207 AV_PIX_FMT_GBRAP12, AV_PIX_FMT_GBRAP16,
210 AV_PIX_FMT_GRAY8, AV_PIX_FMT_GRAY9,
211 AV_PIX_FMT_GRAY10, AV_PIX_FMT_GRAY12,
212 AV_PIX_FMT_GRAY14, AV_PIX_FMT_GRAY16,
216 static const enum AVPixelFormat alpha_pix_fmts[] = {
217 AV_PIX_FMT_YUVA444P, AV_PIX_FMT_YUVA444P9,
218 AV_PIX_FMT_YUVA444P10, AV_PIX_FMT_YUVA444P12,
219 AV_PIX_FMT_YUVA444P16,
220 AV_PIX_FMT_YUVA422P, AV_PIX_FMT_YUVA422P9,
221 AV_PIX_FMT_YUVA422P10, AV_PIX_FMT_YUVA422P12,
222 AV_PIX_FMT_YUVA422P16,
223 AV_PIX_FMT_YUVA420P, AV_PIX_FMT_YUVA420P9,
224 AV_PIX_FMT_YUVA420P10, AV_PIX_FMT_YUVA420P16,
225 AV_PIX_FMT_GBRAP, AV_PIX_FMT_GBRAP10,
226 AV_PIX_FMT_GBRAP12, AV_PIX_FMT_GBRAP16,
230 AVFilterFormats *fmts_list = ff_make_format_list(s->alpha ? alpha_pix_fmts : pix_fmts);
232 return AVERROR(ENOMEM);
233 return ff_set_common_formats(ctx, fmts_list);
236 #define DEFINE_REMAP1_LINE(bits, div) \
237 static void remap1_##bits##bit_line_c(uint8_t *dst, int width, const uint8_t *const src, \
238 ptrdiff_t in_linesize, \
239 const int16_t *const u, const int16_t *const v, \
240 const int16_t *const ker) \
242 const uint##bits##_t *const s = (const uint##bits##_t *const)src; \
243 uint##bits##_t *d = (uint##bits##_t *)dst; \
245 in_linesize /= div; \
247 for (int x = 0; x < width; x++) \
248 d[x] = s[v[x] * in_linesize + u[x]]; \
251 DEFINE_REMAP1_LINE( 8, 1)
252 DEFINE_REMAP1_LINE(16, 2)
255 * Generate remapping function with a given window size and pixel depth.
257 * @param ws size of interpolation window
258 * @param bits number of bits per pixel
260 #define DEFINE_REMAP(ws, bits) \
261 static int remap##ws##_##bits##bit_slice(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs) \
263 ThreadData *td = arg; \
264 const V360Context *s = ctx->priv; \
265 const AVFrame *in = td->in; \
266 AVFrame *out = td->out; \
268 for (int stereo = 0; stereo < 1 + s->out_stereo > STEREO_2D; stereo++) { \
269 for (int plane = 0; plane < s->nb_planes; plane++) { \
270 const unsigned map = s->map[plane]; \
271 const int in_linesize = in->linesize[plane]; \
272 const int out_linesize = out->linesize[plane]; \
273 const int uv_linesize = s->uv_linesize[plane]; \
274 const int in_offset_w = stereo ? s->in_offset_w[plane] : 0; \
275 const int in_offset_h = stereo ? s->in_offset_h[plane] : 0; \
276 const int out_offset_w = stereo ? s->out_offset_w[plane] : 0; \
277 const int out_offset_h = stereo ? s->out_offset_h[plane] : 0; \
278 const uint8_t *const src = in->data[plane] + \
279 in_offset_h * in_linesize + in_offset_w * (bits >> 3); \
280 uint8_t *dst = out->data[plane] + out_offset_h * out_linesize + out_offset_w * (bits >> 3); \
281 const uint8_t *mask = plane == 3 ? s->mask : NULL; \
282 const int width = s->pr_width[plane]; \
283 const int height = s->pr_height[plane]; \
285 const int slice_start = (height * jobnr ) / nb_jobs; \
286 const int slice_end = (height * (jobnr + 1)) / nb_jobs; \
288 for (int y = slice_start; y < slice_end; y++) { \
289 const int16_t *const u = s->u[map] + y * uv_linesize * ws * ws; \
290 const int16_t *const v = s->v[map] + y * uv_linesize * ws * ws; \
291 const int16_t *const ker = s->ker[map] + y * uv_linesize * ws * ws; \
293 s->remap_line(dst + y * out_linesize, width, src, in_linesize, u, v, ker); \
296 for (int y = slice_start; y < slice_end && mask; y++) { \
297 memcpy(dst + y * out_linesize, mask + y * width * (bits >> 3), width * (bits >> 3)); \
312 #define DEFINE_REMAP_LINE(ws, bits, div) \
313 static void remap##ws##_##bits##bit_line_c(uint8_t *dst, int width, const uint8_t *const src, \
314 ptrdiff_t in_linesize, \
315 const int16_t *const u, const int16_t *const v, \
316 const int16_t *const ker) \
318 const uint##bits##_t *const s = (const uint##bits##_t *const)src; \
319 uint##bits##_t *d = (uint##bits##_t *)dst; \
321 in_linesize /= div; \
323 for (int x = 0; x < width; x++) { \
324 const int16_t *const uu = u + x * ws * ws; \
325 const int16_t *const vv = v + x * ws * ws; \
326 const int16_t *const kker = ker + x * ws * ws; \
329 for (int i = 0; i < ws; i++) { \
330 for (int j = 0; j < ws; j++) { \
331 tmp += kker[i * ws + j] * s[vv[i * ws + j] * in_linesize + uu[i * ws + j]]; \
335 d[x] = av_clip_uint##bits(tmp >> 14); \
339 DEFINE_REMAP_LINE(2, 8, 1)
340 DEFINE_REMAP_LINE(4, 8, 1)
341 DEFINE_REMAP_LINE(2, 16, 2)
342 DEFINE_REMAP_LINE(4, 16, 2)
344 void ff_v360_init(V360Context *s, int depth)
348 s->remap_line = depth <= 8 ? remap1_8bit_line_c : remap1_16bit_line_c;
351 s->remap_line = depth <= 8 ? remap2_8bit_line_c : remap2_16bit_line_c;
357 s->remap_line = depth <= 8 ? remap4_8bit_line_c : remap4_16bit_line_c;
362 ff_v360_init_x86(s, depth);
366 * Save nearest pixel coordinates for remapping.
368 * @param du horizontal relative coordinate
369 * @param dv vertical relative coordinate
370 * @param rmap calculated 4x4 window
371 * @param u u remap data
372 * @param v v remap data
373 * @param ker ker remap data
375 static void nearest_kernel(float du, float dv, const XYRemap *rmap,
376 int16_t *u, int16_t *v, int16_t *ker)
378 const int i = lrintf(dv) + 1;
379 const int j = lrintf(du) + 1;
381 u[0] = rmap->u[i][j];
382 v[0] = rmap->v[i][j];
386 * Calculate kernel for bilinear interpolation.
388 * @param du horizontal relative coordinate
389 * @param dv vertical relative coordinate
390 * @param rmap calculated 4x4 window
391 * @param u u remap data
392 * @param v v remap data
393 * @param ker ker remap data
395 static void bilinear_kernel(float du, float dv, const XYRemap *rmap,
396 int16_t *u, int16_t *v, int16_t *ker)
398 for (int i = 0; i < 2; i++) {
399 for (int j = 0; j < 2; j++) {
400 u[i * 2 + j] = rmap->u[i + 1][j + 1];
401 v[i * 2 + j] = rmap->v[i + 1][j + 1];
405 ker[0] = lrintf((1.f - du) * (1.f - dv) * 16385.f);
406 ker[1] = lrintf( du * (1.f - dv) * 16385.f);
407 ker[2] = lrintf((1.f - du) * dv * 16385.f);
408 ker[3] = lrintf( du * dv * 16385.f);
412 * Calculate 1-dimensional cubic coefficients.
414 * @param t relative coordinate
415 * @param coeffs coefficients
417 static inline void calculate_bicubic_coeffs(float t, float *coeffs)
419 const float tt = t * t;
420 const float ttt = t * t * t;
422 coeffs[0] = - t / 3.f + tt / 2.f - ttt / 6.f;
423 coeffs[1] = 1.f - t / 2.f - tt + ttt / 2.f;
424 coeffs[2] = t + tt / 2.f - ttt / 2.f;
425 coeffs[3] = - t / 6.f + ttt / 6.f;
429 * Calculate kernel for bicubic interpolation.
431 * @param du horizontal relative coordinate
432 * @param dv vertical relative coordinate
433 * @param rmap calculated 4x4 window
434 * @param u u remap data
435 * @param v v remap data
436 * @param ker ker remap data
438 static void bicubic_kernel(float du, float dv, const XYRemap *rmap,
439 int16_t *u, int16_t *v, int16_t *ker)
444 calculate_bicubic_coeffs(du, du_coeffs);
445 calculate_bicubic_coeffs(dv, dv_coeffs);
447 for (int i = 0; i < 4; i++) {
448 for (int j = 0; j < 4; j++) {
449 u[i * 4 + j] = rmap->u[i][j];
450 v[i * 4 + j] = rmap->v[i][j];
451 ker[i * 4 + j] = lrintf(du_coeffs[j] * dv_coeffs[i] * 16385.f);
457 * Calculate 1-dimensional lanczos coefficients.
459 * @param t relative coordinate
460 * @param coeffs coefficients
462 static inline void calculate_lanczos_coeffs(float t, float *coeffs)
466 for (int i = 0; i < 4; i++) {
467 const float x = M_PI * (t - i + 1);
471 coeffs[i] = sinf(x) * sinf(x / 2.f) / (x * x / 2.f);
476 for (int i = 0; i < 4; i++) {
482 * Calculate kernel for lanczos interpolation.
484 * @param du horizontal relative coordinate
485 * @param dv vertical relative coordinate
486 * @param rmap calculated 4x4 window
487 * @param u u remap data
488 * @param v v remap data
489 * @param ker ker remap data
491 static void lanczos_kernel(float du, float dv, const XYRemap *rmap,
492 int16_t *u, int16_t *v, int16_t *ker)
497 calculate_lanczos_coeffs(du, du_coeffs);
498 calculate_lanczos_coeffs(dv, dv_coeffs);
500 for (int i = 0; i < 4; i++) {
501 for (int j = 0; j < 4; j++) {
502 u[i * 4 + j] = rmap->u[i][j];
503 v[i * 4 + j] = rmap->v[i][j];
504 ker[i * 4 + j] = lrintf(du_coeffs[j] * dv_coeffs[i] * 16385.f);
510 * Calculate 1-dimensional spline16 coefficients.
512 * @param t relative coordinate
513 * @param coeffs coefficients
515 static void calculate_spline16_coeffs(float t, float *coeffs)
517 coeffs[0] = ((-1.f / 3.f * t + 0.8f) * t - 7.f / 15.f) * t;
518 coeffs[1] = ((t - 9.f / 5.f) * t - 0.2f) * t + 1.f;
519 coeffs[2] = ((6.f / 5.f - t) * t + 0.8f) * t;
520 coeffs[3] = ((1.f / 3.f * t - 0.2f) * t - 2.f / 15.f) * t;
524 * Calculate kernel for spline16 interpolation.
526 * @param du horizontal relative coordinate
527 * @param dv vertical relative coordinate
528 * @param rmap calculated 4x4 window
529 * @param u u remap data
530 * @param v v remap data
531 * @param ker ker remap data
533 static void spline16_kernel(float du, float dv, const XYRemap *rmap,
534 int16_t *u, int16_t *v, int16_t *ker)
539 calculate_spline16_coeffs(du, du_coeffs);
540 calculate_spline16_coeffs(dv, dv_coeffs);
542 for (int i = 0; i < 4; i++) {
543 for (int j = 0; j < 4; j++) {
544 u[i * 4 + j] = rmap->u[i][j];
545 v[i * 4 + j] = rmap->v[i][j];
546 ker[i * 4 + j] = lrintf(du_coeffs[j] * dv_coeffs[i] * 16385.f);
552 * Calculate 1-dimensional gaussian coefficients.
554 * @param t relative coordinate
555 * @param coeffs coefficients
557 static void calculate_gaussian_coeffs(float t, float *coeffs)
561 for (int i = 0; i < 4; i++) {
562 const float x = t - (i - 1);
566 coeffs[i] = expf(-2.f * x * x) * expf(-x * x / 2.f);
571 for (int i = 0; i < 4; i++) {
577 * Calculate kernel for gaussian interpolation.
579 * @param du horizontal relative coordinate
580 * @param dv vertical relative coordinate
581 * @param rmap calculated 4x4 window
582 * @param u u remap data
583 * @param v v remap data
584 * @param ker ker remap data
586 static void gaussian_kernel(float du, float dv, const XYRemap *rmap,
587 int16_t *u, int16_t *v, int16_t *ker)
592 calculate_gaussian_coeffs(du, du_coeffs);
593 calculate_gaussian_coeffs(dv, dv_coeffs);
595 for (int i = 0; i < 4; i++) {
596 for (int j = 0; j < 4; j++) {
597 u[i * 4 + j] = rmap->u[i][j];
598 v[i * 4 + j] = rmap->v[i][j];
599 ker[i * 4 + j] = lrintf(du_coeffs[j] * dv_coeffs[i] * 16385.f);
605 * Modulo operation with only positive remainders.
610 * @return positive remainder of (a / b)
612 static inline int mod(int a, int b)
614 const int res = a % b;
623 * Convert char to corresponding direction.
624 * Used for cubemap options.
626 static int get_direction(char c)
647 * Convert char to corresponding rotation angle.
648 * Used for cubemap options.
650 static int get_rotation(char c)
667 * Convert char to corresponding rotation order.
669 static int get_rorder(char c)
687 * Prepare data for processing cubemap input format.
689 * @param ctx filter context
693 static int prepare_cube_in(AVFilterContext *ctx)
695 V360Context *s = ctx->priv;
697 for (int face = 0; face < NB_FACES; face++) {
698 const char c = s->in_forder[face];
702 av_log(ctx, AV_LOG_ERROR,
703 "Incomplete in_forder option. Direction for all 6 faces should be specified.\n");
704 return AVERROR(EINVAL);
707 direction = get_direction(c);
708 if (direction == -1) {
709 av_log(ctx, AV_LOG_ERROR,
710 "Incorrect direction symbol '%c' in in_forder option.\n", c);
711 return AVERROR(EINVAL);
714 s->in_cubemap_face_order[direction] = face;
717 for (int face = 0; face < NB_FACES; face++) {
718 const char c = s->in_frot[face];
722 av_log(ctx, AV_LOG_ERROR,
723 "Incomplete in_frot option. Rotation for all 6 faces should be specified.\n");
724 return AVERROR(EINVAL);
727 rotation = get_rotation(c);
728 if (rotation == -1) {
729 av_log(ctx, AV_LOG_ERROR,
730 "Incorrect rotation symbol '%c' in in_frot option.\n", c);
731 return AVERROR(EINVAL);
734 s->in_cubemap_face_rotation[face] = rotation;
741 * Prepare data for processing cubemap output format.
743 * @param ctx filter context
747 static int prepare_cube_out(AVFilterContext *ctx)
749 V360Context *s = ctx->priv;
751 for (int face = 0; face < NB_FACES; face++) {
752 const char c = s->out_forder[face];
756 av_log(ctx, AV_LOG_ERROR,
757 "Incomplete out_forder option. Direction for all 6 faces should be specified.\n");
758 return AVERROR(EINVAL);
761 direction = get_direction(c);
762 if (direction == -1) {
763 av_log(ctx, AV_LOG_ERROR,
764 "Incorrect direction symbol '%c' in out_forder option.\n", c);
765 return AVERROR(EINVAL);
768 s->out_cubemap_direction_order[face] = direction;
771 for (int face = 0; face < NB_FACES; face++) {
772 const char c = s->out_frot[face];
776 av_log(ctx, AV_LOG_ERROR,
777 "Incomplete out_frot option. Rotation for all 6 faces should be specified.\n");
778 return AVERROR(EINVAL);
781 rotation = get_rotation(c);
782 if (rotation == -1) {
783 av_log(ctx, AV_LOG_ERROR,
784 "Incorrect rotation symbol '%c' in out_frot option.\n", c);
785 return AVERROR(EINVAL);
788 s->out_cubemap_face_rotation[face] = rotation;
794 static inline void rotate_cube_face(float *uf, float *vf, int rotation)
820 static inline void rotate_cube_face_inverse(float *uf, float *vf, int rotation)
851 static void normalize_vector(float *vec)
853 const float norm = sqrtf(vec[0] * vec[0] + vec[1] * vec[1] + vec[2] * vec[2]);
861 * Calculate 3D coordinates on sphere for corresponding cubemap position.
862 * Common operation for every cubemap.
864 * @param s filter private context
865 * @param uf horizontal cubemap coordinate [0, 1)
866 * @param vf vertical cubemap coordinate [0, 1)
867 * @param face face of cubemap
868 * @param vec coordinates on sphere
869 * @param scalew scale for uf
870 * @param scaleh scale for vf
872 static void cube_to_xyz(const V360Context *s,
873 float uf, float vf, int face,
874 float *vec, float scalew, float scaleh)
876 const int direction = s->out_cubemap_direction_order[face];
882 rotate_cube_face_inverse(&uf, &vf, s->out_cubemap_face_rotation[face]);
923 normalize_vector(vec);
927 * Calculate cubemap position for corresponding 3D coordinates on sphere.
928 * Common operation for every cubemap.
930 * @param s filter private context
931 * @param vec coordinated on sphere
932 * @param uf horizontal cubemap coordinate [0, 1)
933 * @param vf vertical cubemap coordinate [0, 1)
934 * @param direction direction of view
936 static void xyz_to_cube(const V360Context *s,
938 float *uf, float *vf, int *direction)
940 const float phi = atan2f(vec[0], -vec[2]);
941 const float theta = asinf(-vec[1]);
942 float phi_norm, theta_threshold;
945 if (phi >= -M_PI_4 && phi < M_PI_4) {
948 } else if (phi >= -(M_PI_2 + M_PI_4) && phi < -M_PI_4) {
950 phi_norm = phi + M_PI_2;
951 } else if (phi >= M_PI_4 && phi < M_PI_2 + M_PI_4) {
953 phi_norm = phi - M_PI_2;
956 phi_norm = phi + ((phi > 0.f) ? -M_PI : M_PI);
959 theta_threshold = atanf(cosf(phi_norm));
960 if (theta > theta_threshold) {
962 } else if (theta < -theta_threshold) {
966 switch (*direction) {
968 *uf = vec[2] / vec[0];
969 *vf = -vec[1] / vec[0];
972 *uf = vec[2] / vec[0];
973 *vf = vec[1] / vec[0];
976 *uf = vec[0] / vec[1];
977 *vf = -vec[2] / vec[1];
980 *uf = -vec[0] / vec[1];
981 *vf = -vec[2] / vec[1];
984 *uf = -vec[0] / vec[2];
985 *vf = vec[1] / vec[2];
988 *uf = -vec[0] / vec[2];
989 *vf = -vec[1] / vec[2];
995 face = s->in_cubemap_face_order[*direction];
996 rotate_cube_face(uf, vf, s->in_cubemap_face_rotation[face]);
998 (*uf) *= s->input_mirror_modifier[0];
999 (*vf) *= s->input_mirror_modifier[1];
1003 * Find position on another cube face in case of overflow/underflow.
1004 * Used for calculation of interpolation window.
1006 * @param s filter private context
1007 * @param uf horizontal cubemap coordinate
1008 * @param vf vertical cubemap coordinate
1009 * @param direction direction of view
1010 * @param new_uf new horizontal cubemap coordinate
1011 * @param new_vf new vertical cubemap coordinate
1012 * @param face face position on cubemap
1014 static void process_cube_coordinates(const V360Context *s,
1015 float uf, float vf, int direction,
1016 float *new_uf, float *new_vf, int *face)
1019 * Cubemap orientation
1026 * +-------+-------+-------+-------+ ^ e |
1028 * | left | front | right | back | | g |
1029 * +-------+-------+-------+-------+ v h v
1035 *face = s->in_cubemap_face_order[direction];
1036 rotate_cube_face_inverse(&uf, &vf, s->in_cubemap_face_rotation[*face]);
1038 if ((uf < -1.f || uf >= 1.f) && (vf < -1.f || vf >= 1.f)) {
1039 // There are no pixels to use in this case
1042 } else if (uf < -1.f) {
1044 switch (direction) {
1078 } else if (uf >= 1.f) {
1080 switch (direction) {
1114 } else if (vf < -1.f) {
1116 switch (direction) {
1150 } else if (vf >= 1.f) {
1152 switch (direction) {
1192 *face = s->in_cubemap_face_order[direction];
1193 rotate_cube_face(new_uf, new_vf, s->in_cubemap_face_rotation[*face]);
1197 * Calculate 3D coordinates on sphere for corresponding frame position in cubemap3x2 format.
1199 * @param s filter private context
1200 * @param i horizontal position on frame [0, width)
1201 * @param j vertical position on frame [0, height)
1202 * @param width frame width
1203 * @param height frame height
1204 * @param vec coordinates on sphere
1206 static int cube3x2_to_xyz(const V360Context *s,
1207 int i, int j, int width, int height,
1210 const float scalew = s->fout_pad > 0 ? 1.f - s->fout_pad / (s->out_width / 3.f) : 1.f - s->out_pad;
1211 const float scaleh = s->fout_pad > 0 ? 1.f - s->fout_pad / (s->out_height / 2.f) : 1.f - s->out_pad;
1213 const float ew = width / 3.f;
1214 const float eh = height / 2.f;
1216 const int u_face = floorf(i / ew);
1217 const int v_face = floorf(j / eh);
1218 const int face = u_face + 3 * v_face;
1220 const int u_shift = ceilf(ew * u_face);
1221 const int v_shift = ceilf(eh * v_face);
1222 const int ewi = ceilf(ew * (u_face + 1)) - u_shift;
1223 const int ehi = ceilf(eh * (v_face + 1)) - v_shift;
1225 const float uf = 2.f * (i - u_shift + 0.5f) / ewi - 1.f;
1226 const float vf = 2.f * (j - v_shift + 0.5f) / ehi - 1.f;
1228 cube_to_xyz(s, uf, vf, face, vec, scalew, scaleh);
1234 * Calculate frame position in cubemap3x2 format for corresponding 3D coordinates on sphere.
1236 * @param s filter private context
1237 * @param vec coordinates on sphere
1238 * @param width frame width
1239 * @param height frame height
1240 * @param us horizontal coordinates for interpolation window
1241 * @param vs vertical coordinates for interpolation window
1242 * @param du horizontal relative coordinate
1243 * @param dv vertical relative coordinate
1245 static int xyz_to_cube3x2(const V360Context *s,
1246 const float *vec, int width, int height,
1247 int16_t us[4][4], int16_t vs[4][4], float *du, float *dv)
1249 const float scalew = s->fin_pad > 0 ? 1.f - s->fin_pad / (s->in_width / 3.f) : 1.f - s->in_pad;
1250 const float scaleh = s->fin_pad > 0 ? 1.f - s->fin_pad / (s->in_height / 2.f) : 1.f - s->in_pad;
1251 const float ew = width / 3.f;
1252 const float eh = height / 2.f;
1256 int direction, face;
1259 xyz_to_cube(s, vec, &uf, &vf, &direction);
1264 face = s->in_cubemap_face_order[direction];
1267 ewi = ceilf(ew * (u_face + 1)) - ceilf(ew * u_face);
1268 ehi = ceilf(eh * (v_face + 1)) - ceilf(eh * v_face);
1270 uf = 0.5f * ewi * (uf + 1.f) - 0.5f;
1271 vf = 0.5f * ehi * (vf + 1.f) - 0.5f;
1279 for (int i = -1; i < 3; i++) {
1280 for (int j = -1; j < 3; j++) {
1281 int new_ui = ui + j;
1282 int new_vi = vi + i;
1283 int u_shift, v_shift;
1284 int new_ewi, new_ehi;
1286 if (new_ui >= 0 && new_ui < ewi && new_vi >= 0 && new_vi < ehi) {
1287 face = s->in_cubemap_face_order[direction];
1291 u_shift = ceilf(ew * u_face);
1292 v_shift = ceilf(eh * v_face);
1294 uf = 2.f * new_ui / ewi - 1.f;
1295 vf = 2.f * new_vi / ehi - 1.f;
1300 process_cube_coordinates(s, uf, vf, direction, &uf, &vf, &face);
1307 u_shift = ceilf(ew * u_face);
1308 v_shift = ceilf(eh * v_face);
1309 new_ewi = ceilf(ew * (u_face + 1)) - u_shift;
1310 new_ehi = ceilf(eh * (v_face + 1)) - v_shift;
1312 new_ui = av_clip(lrintf(0.5f * new_ewi * (uf + 1.f)), 0, new_ewi - 1);
1313 new_vi = av_clip(lrintf(0.5f * new_ehi * (vf + 1.f)), 0, new_ehi - 1);
1316 us[i + 1][j + 1] = u_shift + new_ui;
1317 vs[i + 1][j + 1] = v_shift + new_vi;
1325 * Calculate 3D coordinates on sphere for corresponding frame position in cubemap1x6 format.
1327 * @param s filter private context
1328 * @param i horizontal position on frame [0, width)
1329 * @param j vertical position on frame [0, height)
1330 * @param width frame width
1331 * @param height frame height
1332 * @param vec coordinates on sphere
1334 static int cube1x6_to_xyz(const V360Context *s,
1335 int i, int j, int width, int height,
1338 const float scalew = s->fout_pad > 0 ? 1.f - (float)(s->fout_pad) / s->out_width : 1.f - s->out_pad;
1339 const float scaleh = s->fout_pad > 0 ? 1.f - s->fout_pad / (s->out_height / 6.f) : 1.f - s->out_pad;
1341 const float ew = width;
1342 const float eh = height / 6.f;
1344 const int face = floorf(j / eh);
1346 const int v_shift = ceilf(eh * face);
1347 const int ehi = ceilf(eh * (face + 1)) - v_shift;
1349 const float uf = 2.f * (i + 0.5f) / ew - 1.f;
1350 const float vf = 2.f * (j - v_shift + 0.5f) / ehi - 1.f;
1352 cube_to_xyz(s, uf, vf, face, vec, scalew, scaleh);
1358 * Calculate 3D coordinates on sphere for corresponding frame position in cubemap6x1 format.
1360 * @param s filter private context
1361 * @param i horizontal position on frame [0, width)
1362 * @param j vertical position on frame [0, height)
1363 * @param width frame width
1364 * @param height frame height
1365 * @param vec coordinates on sphere
1367 static int cube6x1_to_xyz(const V360Context *s,
1368 int i, int j, int width, int height,
1371 const float scalew = s->fout_pad > 0 ? 1.f - s->fout_pad / (s->out_width / 6.f) : 1.f - s->out_pad;
1372 const float scaleh = s->fout_pad > 0 ? 1.f - (float)(s->fout_pad) / s->out_height : 1.f - s->out_pad;
1374 const float ew = width / 6.f;
1375 const float eh = height;
1377 const int face = floorf(i / ew);
1379 const int u_shift = ceilf(ew * face);
1380 const int ewi = ceilf(ew * (face + 1)) - u_shift;
1382 const float uf = 2.f * (i - u_shift + 0.5f) / ewi - 1.f;
1383 const float vf = 2.f * (j + 0.5f) / eh - 1.f;
1385 cube_to_xyz(s, uf, vf, face, vec, scalew, scaleh);
1391 * Calculate frame position in cubemap1x6 format for corresponding 3D coordinates on sphere.
1393 * @param s filter private context
1394 * @param vec coordinates on sphere
1395 * @param width frame width
1396 * @param height frame height
1397 * @param us horizontal coordinates for interpolation window
1398 * @param vs vertical coordinates for interpolation window
1399 * @param du horizontal relative coordinate
1400 * @param dv vertical relative coordinate
1402 static int xyz_to_cube1x6(const V360Context *s,
1403 const float *vec, int width, int height,
1404 int16_t us[4][4], int16_t vs[4][4], float *du, float *dv)
1406 const float scalew = s->fin_pad > 0 ? 1.f - (float)(s->fin_pad) / s->in_width : 1.f - s->in_pad;
1407 const float scaleh = s->fin_pad > 0 ? 1.f - s->fin_pad / (s->in_height / 6.f) : 1.f - s->in_pad;
1408 const float eh = height / 6.f;
1409 const int ewi = width;
1413 int direction, face;
1415 xyz_to_cube(s, vec, &uf, &vf, &direction);
1420 face = s->in_cubemap_face_order[direction];
1421 ehi = ceilf(eh * (face + 1)) - ceilf(eh * face);
1423 uf = 0.5f * ewi * (uf + 1.f) - 0.5f;
1424 vf = 0.5f * ehi * (vf + 1.f) - 0.5f;
1432 for (int i = -1; i < 3; i++) {
1433 for (int j = -1; j < 3; j++) {
1434 int new_ui = ui + j;
1435 int new_vi = vi + i;
1439 if (new_ui >= 0 && new_ui < ewi && new_vi >= 0 && new_vi < ehi) {
1440 face = s->in_cubemap_face_order[direction];
1442 v_shift = ceilf(eh * face);
1444 uf = 2.f * new_ui / ewi - 1.f;
1445 vf = 2.f * new_vi / ehi - 1.f;
1450 process_cube_coordinates(s, uf, vf, direction, &uf, &vf, &face);
1455 v_shift = ceilf(eh * face);
1456 new_ehi = ceilf(eh * (face + 1)) - v_shift;
1458 new_ui = av_clip(lrintf(0.5f * ewi * (uf + 1.f)), 0, ewi - 1);
1459 new_vi = av_clip(lrintf(0.5f * new_ehi * (vf + 1.f)), 0, new_ehi - 1);
1462 us[i + 1][j + 1] = new_ui;
1463 vs[i + 1][j + 1] = v_shift + new_vi;
1471 * Calculate frame position in cubemap6x1 format for corresponding 3D coordinates on sphere.
1473 * @param s filter private context
1474 * @param vec coordinates on sphere
1475 * @param width frame width
1476 * @param height frame height
1477 * @param us horizontal coordinates for interpolation window
1478 * @param vs vertical coordinates for interpolation window
1479 * @param du horizontal relative coordinate
1480 * @param dv vertical relative coordinate
1482 static int xyz_to_cube6x1(const V360Context *s,
1483 const float *vec, int width, int height,
1484 int16_t us[4][4], int16_t vs[4][4], float *du, float *dv)
1486 const float scalew = s->fin_pad > 0 ? 1.f - s->fin_pad / (s->in_width / 6.f) : 1.f - s->in_pad;
1487 const float scaleh = s->fin_pad > 0 ? 1.f - (float)(s->fin_pad) / s->in_height : 1.f - s->in_pad;
1488 const float ew = width / 6.f;
1489 const int ehi = height;
1493 int direction, face;
1495 xyz_to_cube(s, vec, &uf, &vf, &direction);
1500 face = s->in_cubemap_face_order[direction];
1501 ewi = ceilf(ew * (face + 1)) - ceilf(ew * face);
1503 uf = 0.5f * ewi * (uf + 1.f) - 0.5f;
1504 vf = 0.5f * ehi * (vf + 1.f) - 0.5f;
1512 for (int i = -1; i < 3; i++) {
1513 for (int j = -1; j < 3; j++) {
1514 int new_ui = ui + j;
1515 int new_vi = vi + i;
1519 if (new_ui >= 0 && new_ui < ewi && new_vi >= 0 && new_vi < ehi) {
1520 face = s->in_cubemap_face_order[direction];
1522 u_shift = ceilf(ew * face);
1524 uf = 2.f * new_ui / ewi - 1.f;
1525 vf = 2.f * new_vi / ehi - 1.f;
1530 process_cube_coordinates(s, uf, vf, direction, &uf, &vf, &face);
1535 u_shift = ceilf(ew * face);
1536 new_ewi = ceilf(ew * (face + 1)) - u_shift;
1538 new_ui = av_clip(lrintf(0.5f * new_ewi * (uf + 1.f)), 0, new_ewi - 1);
1539 new_vi = av_clip(lrintf(0.5f * ehi * (vf + 1.f)), 0, ehi - 1);
1542 us[i + 1][j + 1] = u_shift + new_ui;
1543 vs[i + 1][j + 1] = new_vi;
1551 * Calculate 3D coordinates on sphere for corresponding frame position in equirectangular format.
1553 * @param s filter private context
1554 * @param i horizontal position on frame [0, width)
1555 * @param j vertical position on frame [0, height)
1556 * @param width frame width
1557 * @param height frame height
1558 * @param vec coordinates on sphere
1560 static int equirect_to_xyz(const V360Context *s,
1561 int i, int j, int width, int height,
1564 const float phi = ((2.f * i) / width - 1.f) * M_PI;
1565 const float theta = ((2.f * j) / height - 1.f) * M_PI_2;
1567 const float sin_phi = sinf(phi);
1568 const float cos_phi = cosf(phi);
1569 const float sin_theta = sinf(theta);
1570 const float cos_theta = cosf(theta);
1572 vec[0] = cos_theta * sin_phi;
1573 vec[1] = -sin_theta;
1574 vec[2] = -cos_theta * cos_phi;
1580 * Prepare data for processing stereographic output format.
1582 * @param ctx filter context
1584 * @return error code
1586 static int prepare_stereographic_out(AVFilterContext *ctx)
1588 V360Context *s = ctx->priv;
1590 s->flat_range[0] = tanf(FFMIN(s->h_fov, 359.f) * M_PI / 720.f);
1591 s->flat_range[1] = tanf(FFMIN(s->v_fov, 359.f) * M_PI / 720.f);
1597 * Calculate 3D coordinates on sphere for corresponding frame position in stereographic format.
1599 * @param s filter private context
1600 * @param i horizontal position on frame [0, width)
1601 * @param j vertical position on frame [0, height)
1602 * @param width frame width
1603 * @param height frame height
1604 * @param vec coordinates on sphere
1606 static int stereographic_to_xyz(const V360Context *s,
1607 int i, int j, int width, int height,
1610 const float x = ((2.f * i) / width - 1.f) * s->flat_range[0];
1611 const float y = ((2.f * j) / height - 1.f) * s->flat_range[1];
1612 const float xy = x * x + y * y;
1614 vec[0] = 2.f * x / (1.f + xy);
1615 vec[1] = (-1.f + xy) / (1.f + xy);
1616 vec[2] = 2.f * y / (1.f + xy);
1618 normalize_vector(vec);
1624 * Prepare data for processing stereographic input format.
1626 * @param ctx filter context
1628 * @return error code
1630 static int prepare_stereographic_in(AVFilterContext *ctx)
1632 V360Context *s = ctx->priv;
1634 s->iflat_range[0] = tanf(FFMIN(s->ih_fov, 359.f) * M_PI / 720.f);
1635 s->iflat_range[1] = tanf(FFMIN(s->iv_fov, 359.f) * M_PI / 720.f);
1641 * Calculate frame position in stereographic format for corresponding 3D coordinates on sphere.
1643 * @param s filter private context
1644 * @param vec coordinates on sphere
1645 * @param width frame width
1646 * @param height frame height
1647 * @param us horizontal coordinates for interpolation window
1648 * @param vs vertical coordinates for interpolation window
1649 * @param du horizontal relative coordinate
1650 * @param dv vertical relative coordinate
1652 static int xyz_to_stereographic(const V360Context *s,
1653 const float *vec, int width, int height,
1654 int16_t us[4][4], int16_t vs[4][4], float *du, float *dv)
1656 const float x = vec[0] / (1.f - vec[1]) / s->iflat_range[0] * s->input_mirror_modifier[0];
1657 const float y = vec[2] / (1.f - vec[1]) / s->iflat_range[1] * s->input_mirror_modifier[1];
1659 int visible, ui, vi;
1661 uf = (x + 1.f) * width / 2.f;
1662 vf = (y + 1.f) * height / 2.f;
1666 visible = isfinite(x) && isfinite(y) && vi >= 0 && vi < height && ui >= 0 && ui < width;
1668 *du = visible ? uf - ui : 0.f;
1669 *dv = visible ? vf - vi : 0.f;
1671 for (int i = -1; i < 3; i++) {
1672 for (int j = -1; j < 3; j++) {
1673 us[i + 1][j + 1] = visible ? av_clip(ui + j, 0, width - 1) : 0;
1674 vs[i + 1][j + 1] = visible ? av_clip(vi + i, 0, height - 1) : 0;
1682 * Calculate frame position in equirectangular format for corresponding 3D coordinates on sphere.
1684 * @param s filter private context
1685 * @param vec coordinates on sphere
1686 * @param width frame width
1687 * @param height frame height
1688 * @param us horizontal coordinates for interpolation window
1689 * @param vs vertical coordinates for interpolation window
1690 * @param du horizontal relative coordinate
1691 * @param dv vertical relative coordinate
1693 static int xyz_to_equirect(const V360Context *s,
1694 const float *vec, int width, int height,
1695 int16_t us[4][4], int16_t vs[4][4], float *du, float *dv)
1697 const float phi = atan2f(vec[0], -vec[2]) * s->input_mirror_modifier[0];
1698 const float theta = asinf(-vec[1]) * s->input_mirror_modifier[1];
1702 uf = (phi / M_PI + 1.f) * width / 2.f;
1703 vf = (theta / M_PI_2 + 1.f) * height / 2.f;
1710 for (int i = -1; i < 3; i++) {
1711 for (int j = -1; j < 3; j++) {
1712 us[i + 1][j + 1] = mod(ui + j, width);
1713 vs[i + 1][j + 1] = av_clip(vi + i, 0, height - 1);
1721 * Prepare data for processing flat input format.
1723 * @param ctx filter context
1725 * @return error code
1727 static int prepare_flat_in(AVFilterContext *ctx)
1729 V360Context *s = ctx->priv;
1731 s->iflat_range[0] = tanf(0.5f * s->ih_fov * M_PI / 180.f);
1732 s->iflat_range[1] = tanf(0.5f * s->iv_fov * M_PI / 180.f);
1738 * Calculate frame position in flat format for corresponding 3D coordinates on sphere.
1740 * @param s filter private context
1741 * @param vec coordinates on sphere
1742 * @param width frame width
1743 * @param height frame height
1744 * @param us horizontal coordinates for interpolation window
1745 * @param vs vertical coordinates for interpolation window
1746 * @param du horizontal relative coordinate
1747 * @param dv vertical relative coordinate
1749 static int xyz_to_flat(const V360Context *s,
1750 const float *vec, int width, int height,
1751 int16_t us[4][4], int16_t vs[4][4], float *du, float *dv)
1753 const float theta = acosf(vec[2]);
1754 const float r = tanf(theta);
1755 const float rr = fabsf(r) < 1e+6f ? r : hypotf(width, height);
1756 const float zf = -vec[2];
1757 const float h = hypotf(vec[0], vec[1]);
1758 const float c = h <= 1e-6f ? 1.f : rr / h;
1759 float uf = -vec[0] * c / s->iflat_range[0] * s->input_mirror_modifier[0];
1760 float vf = vec[1] * c / s->iflat_range[1] * s->input_mirror_modifier[1];
1761 int visible, ui, vi;
1763 uf = zf >= 0.f ? (uf + 1.f) * width / 2.f : 0.f;
1764 vf = zf >= 0.f ? (vf + 1.f) * height / 2.f : 0.f;
1769 visible = vi >= 0 && vi < height && ui >= 0 && ui < width;
1774 for (int i = -1; i < 3; i++) {
1775 for (int j = -1; j < 3; j++) {
1776 us[i + 1][j + 1] = visible ? av_clip(ui + j, 0, width - 1) : 0;
1777 vs[i + 1][j + 1] = visible ? av_clip(vi + i, 0, height - 1) : 0;
1785 * Calculate frame position in mercator format for corresponding 3D coordinates on sphere.
1787 * @param s filter private context
1788 * @param vec coordinates on sphere
1789 * @param width frame width
1790 * @param height frame height
1791 * @param us horizontal coordinates for interpolation window
1792 * @param vs vertical coordinates for interpolation window
1793 * @param du horizontal relative coordinate
1794 * @param dv vertical relative coordinate
1796 static int xyz_to_mercator(const V360Context *s,
1797 const float *vec, int width, int height,
1798 int16_t us[4][4], int16_t vs[4][4], float *du, float *dv)
1800 const float phi = atan2f(vec[0], -vec[2]) * s->input_mirror_modifier[0];
1801 const float theta = -vec[1] * s->input_mirror_modifier[1];
1805 uf = (phi / M_PI + 1.f) * width / 2.f;
1806 vf = (av_clipf(logf((1.f + theta) / (1.f - theta)) / (2.f * M_PI), -1.f, 1.f) + 1.f) * height / 2.f;
1813 for (int i = -1; i < 3; i++) {
1814 for (int j = -1; j < 3; j++) {
1815 us[i + 1][j + 1] = av_clip(ui + j, 0, width - 1);
1816 vs[i + 1][j + 1] = av_clip(vi + i, 0, height - 1);
1824 * Calculate 3D coordinates on sphere for corresponding frame position in mercator format.
1826 * @param s filter private context
1827 * @param i horizontal position on frame [0, width)
1828 * @param j vertical position on frame [0, height)
1829 * @param width frame width
1830 * @param height frame height
1831 * @param vec coordinates on sphere
1833 static int mercator_to_xyz(const V360Context *s,
1834 int i, int j, int width, int height,
1837 const float phi = ((2.f * i) / width - 1.f) * M_PI + M_PI_2;
1838 const float y = ((2.f * j) / height - 1.f) * M_PI;
1839 const float div = expf(2.f * y) + 1.f;
1841 const float sin_phi = sinf(phi);
1842 const float cos_phi = cosf(phi);
1843 const float sin_theta = -2.f * expf(y) / div;
1844 const float cos_theta = -(expf(2.f * y) - 1.f) / div;
1846 vec[0] = sin_theta * cos_phi;
1848 vec[2] = sin_theta * sin_phi;
1854 * Calculate frame position in ball format for corresponding 3D coordinates on sphere.
1856 * @param s filter private context
1857 * @param vec coordinates on sphere
1858 * @param width frame width
1859 * @param height frame height
1860 * @param us horizontal coordinates for interpolation window
1861 * @param vs vertical coordinates for interpolation window
1862 * @param du horizontal relative coordinate
1863 * @param dv vertical relative coordinate
1865 static int xyz_to_ball(const V360Context *s,
1866 const float *vec, int width, int height,
1867 int16_t us[4][4], int16_t vs[4][4], float *du, float *dv)
1869 const float l = hypotf(vec[0], vec[1]);
1870 const float r = sqrtf(1.f + vec[2]) / M_SQRT2;
1874 uf = (1.f + r * vec[0] * s->input_mirror_modifier[0] / (l > 0.f ? l : 1.f)) * width * 0.5f;
1875 vf = (1.f - r * vec[1] * s->input_mirror_modifier[1] / (l > 0.f ? l : 1.f)) * height * 0.5f;
1883 for (int i = -1; i < 3; i++) {
1884 for (int j = -1; j < 3; j++) {
1885 us[i + 1][j + 1] = av_clip(ui + j, 0, width - 1);
1886 vs[i + 1][j + 1] = av_clip(vi + i, 0, height - 1);
1894 * Calculate 3D coordinates on sphere for corresponding frame position in ball format.
1896 * @param s filter private context
1897 * @param i horizontal position on frame [0, width)
1898 * @param j vertical position on frame [0, height)
1899 * @param width frame width
1900 * @param height frame height
1901 * @param vec coordinates on sphere
1903 static int ball_to_xyz(const V360Context *s,
1904 int i, int j, int width, int height,
1907 const float x = (2.f * i) / width - 1.f;
1908 const float y = (2.f * j) / height - 1.f;
1909 const float l = hypotf(x, y);
1912 const float z = 2.f * l * sqrtf(1.f - l * l);
1914 vec[0] = z * x / (l > 0.f ? l : 1.f);
1915 vec[1] = -z * y / (l > 0.f ? l : 1.f);
1916 vec[2] = -1.f + 2.f * l * l;
1928 * Calculate 3D coordinates on sphere for corresponding frame position in hammer format.
1930 * @param s filter private context
1931 * @param i horizontal position on frame [0, width)
1932 * @param j vertical position on frame [0, height)
1933 * @param width frame width
1934 * @param height frame height
1935 * @param vec coordinates on sphere
1937 static int hammer_to_xyz(const V360Context *s,
1938 int i, int j, int width, int height,
1941 const float x = ((2.f * i) / width - 1.f);
1942 const float y = ((2.f * j) / height - 1.f);
1944 const float xx = x * x;
1945 const float yy = y * y;
1947 const float z = sqrtf(1.f - xx * 0.5f - yy * 0.5f);
1949 const float a = M_SQRT2 * x * z;
1950 const float b = 2.f * z * z - 1.f;
1952 const float aa = a * a;
1953 const float bb = b * b;
1955 const float w = sqrtf(1.f - 2.f * yy * z * z);
1957 vec[0] = w * 2.f * a * b / (aa + bb);
1958 vec[1] = -M_SQRT2 * y * z;
1959 vec[2] = -w * (bb - aa) / (aa + bb);
1961 normalize_vector(vec);
1967 * Calculate frame position in hammer format for corresponding 3D coordinates on sphere.
1969 * @param s filter private context
1970 * @param vec coordinates on sphere
1971 * @param width frame width
1972 * @param height frame height
1973 * @param us horizontal coordinates for interpolation window
1974 * @param vs vertical coordinates for interpolation window
1975 * @param du horizontal relative coordinate
1976 * @param dv vertical relative coordinate
1978 static int xyz_to_hammer(const V360Context *s,
1979 const float *vec, int width, int height,
1980 int16_t us[4][4], int16_t vs[4][4], float *du, float *dv)
1982 const float theta = atan2f(vec[0], -vec[2]) * s->input_mirror_modifier[0];
1984 const float z = sqrtf(1.f + sqrtf(1.f - vec[1] * vec[1]) * cosf(theta * 0.5f));
1985 const float x = sqrtf(1.f - vec[1] * vec[1]) * sinf(theta * 0.5f) / z;
1986 const float y = -vec[1] / z * s->input_mirror_modifier[1];
1990 uf = (x + 1.f) * width / 2.f;
1991 vf = (y + 1.f) * height / 2.f;
1998 for (int i = -1; i < 3; i++) {
1999 for (int j = -1; j < 3; j++) {
2000 us[i + 1][j + 1] = av_clip(ui + j, 0, width - 1);
2001 vs[i + 1][j + 1] = av_clip(vi + i, 0, height - 1);
2009 * Calculate 3D coordinates on sphere for corresponding frame position in sinusoidal format.
2011 * @param s filter private context
2012 * @param i horizontal position on frame [0, width)
2013 * @param j vertical position on frame [0, height)
2014 * @param width frame width
2015 * @param height frame height
2016 * @param vec coordinates on sphere
2018 static int sinusoidal_to_xyz(const V360Context *s,
2019 int i, int j, int width, int height,
2022 const float theta = ((2.f * j) / height - 1.f) * M_PI_2;
2023 const float phi = ((2.f * i) / width - 1.f) * M_PI / cosf(theta);
2025 const float sin_phi = sinf(phi);
2026 const float cos_phi = cosf(phi);
2027 const float sin_theta = sinf(theta);
2028 const float cos_theta = cosf(theta);
2030 vec[0] = cos_theta * sin_phi;
2031 vec[1] = -sin_theta;
2032 vec[2] = -cos_theta * cos_phi;
2034 normalize_vector(vec);
2040 * Calculate frame position in sinusoidal format for corresponding 3D coordinates on sphere.
2042 * @param s filter private context
2043 * @param vec coordinates on sphere
2044 * @param width frame width
2045 * @param height frame height
2046 * @param us horizontal coordinates for interpolation window
2047 * @param vs vertical coordinates for interpolation window
2048 * @param du horizontal relative coordinate
2049 * @param dv vertical relative coordinate
2051 static int xyz_to_sinusoidal(const V360Context *s,
2052 const float *vec, int width, int height,
2053 int16_t us[4][4], int16_t vs[4][4], float *du, float *dv)
2055 const float theta = asinf(-vec[1]) * s->input_mirror_modifier[1];
2056 const float phi = atan2f(vec[0], -vec[2]) * s->input_mirror_modifier[0] * cosf(theta);
2060 uf = (phi / M_PI + 1.f) * width / 2.f;
2061 vf = (theta / M_PI_2 + 1.f) * height / 2.f;
2068 for (int i = -1; i < 3; i++) {
2069 for (int j = -1; j < 3; j++) {
2070 us[i + 1][j + 1] = av_clip(ui + j, 0, width - 1);
2071 vs[i + 1][j + 1] = av_clip(vi + i, 0, height - 1);
2079 * Prepare data for processing equi-angular cubemap input format.
2081 * @param ctx filter context
2083 * @return error code
2085 static int prepare_eac_in(AVFilterContext *ctx)
2087 V360Context *s = ctx->priv;
2089 if (s->ih_flip && s->iv_flip) {
2090 s->in_cubemap_face_order[RIGHT] = BOTTOM_LEFT;
2091 s->in_cubemap_face_order[LEFT] = BOTTOM_RIGHT;
2092 s->in_cubemap_face_order[UP] = TOP_LEFT;
2093 s->in_cubemap_face_order[DOWN] = TOP_RIGHT;
2094 s->in_cubemap_face_order[FRONT] = BOTTOM_MIDDLE;
2095 s->in_cubemap_face_order[BACK] = TOP_MIDDLE;
2096 } else if (s->ih_flip) {
2097 s->in_cubemap_face_order[RIGHT] = TOP_LEFT;
2098 s->in_cubemap_face_order[LEFT] = TOP_RIGHT;
2099 s->in_cubemap_face_order[UP] = BOTTOM_LEFT;
2100 s->in_cubemap_face_order[DOWN] = BOTTOM_RIGHT;
2101 s->in_cubemap_face_order[FRONT] = TOP_MIDDLE;
2102 s->in_cubemap_face_order[BACK] = BOTTOM_MIDDLE;
2103 } else if (s->iv_flip) {
2104 s->in_cubemap_face_order[RIGHT] = BOTTOM_RIGHT;
2105 s->in_cubemap_face_order[LEFT] = BOTTOM_LEFT;
2106 s->in_cubemap_face_order[UP] = TOP_RIGHT;
2107 s->in_cubemap_face_order[DOWN] = TOP_LEFT;
2108 s->in_cubemap_face_order[FRONT] = BOTTOM_MIDDLE;
2109 s->in_cubemap_face_order[BACK] = TOP_MIDDLE;
2111 s->in_cubemap_face_order[RIGHT] = TOP_RIGHT;
2112 s->in_cubemap_face_order[LEFT] = TOP_LEFT;
2113 s->in_cubemap_face_order[UP] = BOTTOM_RIGHT;
2114 s->in_cubemap_face_order[DOWN] = BOTTOM_LEFT;
2115 s->in_cubemap_face_order[FRONT] = TOP_MIDDLE;
2116 s->in_cubemap_face_order[BACK] = BOTTOM_MIDDLE;
2120 s->in_cubemap_face_rotation[TOP_LEFT] = ROT_270;
2121 s->in_cubemap_face_rotation[TOP_MIDDLE] = ROT_90;
2122 s->in_cubemap_face_rotation[TOP_RIGHT] = ROT_270;
2123 s->in_cubemap_face_rotation[BOTTOM_LEFT] = ROT_0;
2124 s->in_cubemap_face_rotation[BOTTOM_MIDDLE] = ROT_0;
2125 s->in_cubemap_face_rotation[BOTTOM_RIGHT] = ROT_0;
2127 s->in_cubemap_face_rotation[TOP_LEFT] = ROT_0;
2128 s->in_cubemap_face_rotation[TOP_MIDDLE] = ROT_0;
2129 s->in_cubemap_face_rotation[TOP_RIGHT] = ROT_0;
2130 s->in_cubemap_face_rotation[BOTTOM_LEFT] = ROT_270;
2131 s->in_cubemap_face_rotation[BOTTOM_MIDDLE] = ROT_90;
2132 s->in_cubemap_face_rotation[BOTTOM_RIGHT] = ROT_270;
2139 * Prepare data for processing equi-angular cubemap output format.
2141 * @param ctx filter context
2143 * @return error code
2145 static int prepare_eac_out(AVFilterContext *ctx)
2147 V360Context *s = ctx->priv;
2149 s->out_cubemap_direction_order[TOP_LEFT] = LEFT;
2150 s->out_cubemap_direction_order[TOP_MIDDLE] = FRONT;
2151 s->out_cubemap_direction_order[TOP_RIGHT] = RIGHT;
2152 s->out_cubemap_direction_order[BOTTOM_LEFT] = DOWN;
2153 s->out_cubemap_direction_order[BOTTOM_MIDDLE] = BACK;
2154 s->out_cubemap_direction_order[BOTTOM_RIGHT] = UP;
2156 s->out_cubemap_face_rotation[TOP_LEFT] = ROT_0;
2157 s->out_cubemap_face_rotation[TOP_MIDDLE] = ROT_0;
2158 s->out_cubemap_face_rotation[TOP_RIGHT] = ROT_0;
2159 s->out_cubemap_face_rotation[BOTTOM_LEFT] = ROT_270;
2160 s->out_cubemap_face_rotation[BOTTOM_MIDDLE] = ROT_90;
2161 s->out_cubemap_face_rotation[BOTTOM_RIGHT] = ROT_270;
2167 * Calculate 3D coordinates on sphere for corresponding frame position in equi-angular cubemap format.
2169 * @param s filter private context
2170 * @param i horizontal position on frame [0, width)
2171 * @param j vertical position on frame [0, height)
2172 * @param width frame width
2173 * @param height frame height
2174 * @param vec coordinates on sphere
2176 static int eac_to_xyz(const V360Context *s,
2177 int i, int j, int width, int height,
2180 const float pixel_pad = 2;
2181 const float u_pad = pixel_pad / width;
2182 const float v_pad = pixel_pad / height;
2184 int u_face, v_face, face;
2186 float l_x, l_y, l_z;
2188 float uf = (i + 0.5f) / width;
2189 float vf = (j + 0.5f) / height;
2191 // EAC has 2-pixel padding on faces except between faces on the same row
2192 // Padding pixels seems not to be stretched with tangent as regular pixels
2193 // Formulas below approximate original padding as close as I could get experimentally
2195 // Horizontal padding
2196 uf = 3.f * (uf - u_pad) / (1.f - 2.f * u_pad);
2200 } else if (uf >= 3.f) {
2204 u_face = floorf(uf);
2205 uf = fmodf(uf, 1.f) - 0.5f;
2209 v_face = floorf(vf * 2.f);
2210 vf = (vf - v_pad - 0.5f * v_face) / (0.5f - 2.f * v_pad) - 0.5f;
2212 if (uf >= -0.5f && uf < 0.5f) {
2213 uf = tanf(M_PI_2 * uf);
2217 if (vf >= -0.5f && vf < 0.5f) {
2218 vf = tanf(M_PI_2 * vf);
2223 face = u_face + 3 * v_face;
2264 normalize_vector(vec);
2270 * Calculate frame position in equi-angular cubemap format for corresponding 3D coordinates on sphere.
2272 * @param s filter private context
2273 * @param vec coordinates on sphere
2274 * @param width frame width
2275 * @param height frame height
2276 * @param us horizontal coordinates for interpolation window
2277 * @param vs vertical coordinates for interpolation window
2278 * @param du horizontal relative coordinate
2279 * @param dv vertical relative coordinate
2281 static int xyz_to_eac(const V360Context *s,
2282 const float *vec, int width, int height,
2283 int16_t us[4][4], int16_t vs[4][4], float *du, float *dv)
2285 const float pixel_pad = 2;
2286 const float u_pad = pixel_pad / width;
2287 const float v_pad = pixel_pad / height;
2291 int direction, face;
2294 xyz_to_cube(s, vec, &uf, &vf, &direction);
2296 face = s->in_cubemap_face_order[direction];
2300 uf = M_2_PI * atanf(uf) + 0.5f;
2301 vf = M_2_PI * atanf(vf) + 0.5f;
2303 // These formulas are inversed from eac_to_xyz ones
2304 uf = (uf + u_face) * (1.f - 2.f * u_pad) / 3.f + u_pad;
2305 vf = vf * (0.5f - 2.f * v_pad) + v_pad + 0.5f * v_face;
2319 for (int i = -1; i < 3; i++) {
2320 for (int j = -1; j < 3; j++) {
2321 us[i + 1][j + 1] = av_clip(ui + j, 0, width - 1);
2322 vs[i + 1][j + 1] = av_clip(vi + i, 0, height - 1);
2330 * Prepare data for processing flat output format.
2332 * @param ctx filter context
2334 * @return error code
2336 static int prepare_flat_out(AVFilterContext *ctx)
2338 V360Context *s = ctx->priv;
2340 s->flat_range[0] = tanf(0.5f * s->h_fov * M_PI / 180.f);
2341 s->flat_range[1] = tanf(0.5f * s->v_fov * M_PI / 180.f);
2347 * Calculate 3D coordinates on sphere for corresponding frame position in flat format.
2349 * @param s filter private context
2350 * @param i horizontal position on frame [0, width)
2351 * @param j vertical position on frame [0, height)
2352 * @param width frame width
2353 * @param height frame height
2354 * @param vec coordinates on sphere
2356 static int flat_to_xyz(const V360Context *s,
2357 int i, int j, int width, int height,
2360 const float l_x = s->flat_range[0] * (2.f * i / width - 1.f);
2361 const float l_y = -s->flat_range[1] * (2.f * j / height - 1.f);
2367 normalize_vector(vec);
2373 * Prepare data for processing fisheye output format.
2375 * @param ctx filter context
2377 * @return error code
2379 static int prepare_fisheye_out(AVFilterContext *ctx)
2381 V360Context *s = ctx->priv;
2383 s->flat_range[0] = s->h_fov / 180.f;
2384 s->flat_range[1] = s->v_fov / 180.f;
2390 * Calculate 3D coordinates on sphere for corresponding frame position in fisheye format.
2392 * @param s filter private context
2393 * @param i horizontal position on frame [0, width)
2394 * @param j vertical position on frame [0, height)
2395 * @param width frame width
2396 * @param height frame height
2397 * @param vec coordinates on sphere
2399 static int fisheye_to_xyz(const V360Context *s,
2400 int i, int j, int width, int height,
2403 const float uf = s->flat_range[0] * ((2.f * i) / width - 1.f);
2404 const float vf = s->flat_range[1] * ((2.f * j) / height - 1.f);
2406 const float phi = -atan2f(vf, uf);
2407 const float theta = -M_PI_2 * (1.f - hypotf(uf, vf));
2409 vec[0] = cosf(theta) * cosf(phi);
2410 vec[1] = cosf(theta) * sinf(phi);
2411 vec[2] = sinf(theta);
2413 normalize_vector(vec);
2419 * Prepare data for processing fisheye input format.
2421 * @param ctx filter context
2423 * @return error code
2425 static int prepare_fisheye_in(AVFilterContext *ctx)
2427 V360Context *s = ctx->priv;
2429 s->iflat_range[0] = s->ih_fov / 180.f;
2430 s->iflat_range[1] = s->iv_fov / 180.f;
2436 * Calculate frame position in fisheye format for corresponding 3D coordinates on sphere.
2438 * @param s filter private context
2439 * @param vec coordinates on sphere
2440 * @param width frame width
2441 * @param height frame height
2442 * @param us horizontal coordinates for interpolation window
2443 * @param vs vertical coordinates for interpolation window
2444 * @param du horizontal relative coordinate
2445 * @param dv vertical relative coordinate
2447 static int xyz_to_fisheye(const V360Context *s,
2448 const float *vec, int width, int height,
2449 int16_t us[4][4], int16_t vs[4][4], float *du, float *dv)
2451 const float phi = -atan2f(hypotf(vec[0], vec[1]), -vec[2]) / M_PI;
2452 const float theta = -atan2f(vec[0], vec[1]);
2454 float uf = sinf(theta) * phi * s->input_mirror_modifier[0] / s->iflat_range[0];
2455 float vf = cosf(theta) * phi * s->input_mirror_modifier[1] / s->iflat_range[1];
2457 const int visible = hypotf(uf, vf) <= 0.5f;
2460 uf = (uf + 0.5f) * width;
2461 vf = (vf + 0.5f) * height;
2466 *du = visible ? uf - ui : 0.f;
2467 *dv = visible ? vf - vi : 0.f;
2469 for (int i = -1; i < 3; i++) {
2470 for (int j = -1; j < 3; j++) {
2471 us[i + 1][j + 1] = visible ? av_clip(ui + j, 0, width - 1) : 0;
2472 vs[i + 1][j + 1] = visible ? av_clip(vi + i, 0, height - 1) : 0;
2480 * Calculate 3D coordinates on sphere for corresponding frame position in pannini format.
2482 * @param s filter private context
2483 * @param i horizontal position on frame [0, width)
2484 * @param j vertical position on frame [0, height)
2485 * @param width frame width
2486 * @param height frame height
2487 * @param vec coordinates on sphere
2489 static int pannini_to_xyz(const V360Context *s,
2490 int i, int j, int width, int height,
2493 const float uf = ((2.f * i) / width - 1.f);
2494 const float vf = ((2.f * j) / height - 1.f);
2496 const float d = s->h_fov;
2497 const float k = uf * uf / ((d + 1.f) * (d + 1.f));
2498 const float dscr = k * k * d * d - (k + 1.f) * (k * d * d - 1.f);
2499 const float clon = (-k * d + sqrtf(dscr)) / (k + 1.f);
2500 const float S = (d + 1.f) / (d + clon);
2501 const float lon = -(M_PI + atan2f(uf, S * clon));
2502 const float lat = -atan2f(vf, S);
2504 vec[0] = sinf(lon) * cosf(lat);
2506 vec[2] = cosf(lon) * cosf(lat);
2508 normalize_vector(vec);
2514 * Prepare data for processing cylindrical output format.
2516 * @param ctx filter context
2518 * @return error code
2520 static int prepare_cylindrical_out(AVFilterContext *ctx)
2522 V360Context *s = ctx->priv;
2524 s->flat_range[0] = M_PI * s->h_fov / 360.f;
2525 s->flat_range[1] = tanf(0.5f * s->v_fov * M_PI / 180.f);
2531 * Calculate 3D coordinates on sphere for corresponding frame position in cylindrical format.
2533 * @param s filter private context
2534 * @param i horizontal position on frame [0, width)
2535 * @param j vertical position on frame [0, height)
2536 * @param width frame width
2537 * @param height frame height
2538 * @param vec coordinates on sphere
2540 static int cylindrical_to_xyz(const V360Context *s,
2541 int i, int j, int width, int height,
2544 const float uf = s->flat_range[0] * ((2.f * i) / width - 1.f);
2545 const float vf = s->flat_range[1] * ((2.f * j) / height - 1.f);
2547 const float phi = uf;
2548 const float theta = atanf(vf);
2550 const float sin_phi = sinf(phi);
2551 const float cos_phi = cosf(phi);
2552 const float sin_theta = sinf(theta);
2553 const float cos_theta = cosf(theta);
2555 vec[0] = cos_theta * sin_phi;
2556 vec[1] = -sin_theta;
2557 vec[2] = -cos_theta * cos_phi;
2559 normalize_vector(vec);
2565 * Prepare data for processing cylindrical input format.
2567 * @param ctx filter context
2569 * @return error code
2571 static int prepare_cylindrical_in(AVFilterContext *ctx)
2573 V360Context *s = ctx->priv;
2575 s->iflat_range[0] = M_PI * s->ih_fov / 360.f;
2576 s->iflat_range[1] = tanf(0.5f * s->iv_fov * M_PI / 180.f);
2582 * Calculate frame position in cylindrical format for corresponding 3D coordinates on sphere.
2584 * @param s filter private context
2585 * @param vec coordinates on sphere
2586 * @param width frame width
2587 * @param height frame height
2588 * @param us horizontal coordinates for interpolation window
2589 * @param vs vertical coordinates for interpolation window
2590 * @param du horizontal relative coordinate
2591 * @param dv vertical relative coordinate
2593 static int xyz_to_cylindrical(const V360Context *s,
2594 const float *vec, int width, int height,
2595 int16_t us[4][4], int16_t vs[4][4], float *du, float *dv)
2597 const float phi = atan2f(vec[0], -vec[2]) * s->input_mirror_modifier[0] / s->iflat_range[0];
2598 const float theta = atan2f(-vec[1], hypotf(vec[0], vec[2])) * s->input_mirror_modifier[1] / s->iflat_range[1];
2599 int visible, ui, vi;
2602 uf = (phi + 1.f) * (width - 1) / 2.f;
2603 vf = (tanf(theta) + 1.f) * height / 2.f;
2607 visible = vi >= 0 && vi < height && ui >= 0 && ui < width &&
2608 theta <= M_PI * s->iv_fov / 180.f &&
2609 theta >= -M_PI * s->iv_fov / 180.f;
2614 for (int i = -1; i < 3; i++) {
2615 for (int j = -1; j < 3; j++) {
2616 us[i + 1][j + 1] = visible ? av_clip(ui + j, 0, width - 1) : 0;
2617 vs[i + 1][j + 1] = visible ? av_clip(vi + i, 0, height - 1) : 0;
2625 * Calculate 3D coordinates on sphere for corresponding frame position in perspective format.
2627 * @param s filter private context
2628 * @param i horizontal position on frame [0, width)
2629 * @param j vertical position on frame [0, height)
2630 * @param width frame width
2631 * @param height frame height
2632 * @param vec coordinates on sphere
2634 static int perspective_to_xyz(const V360Context *s,
2635 int i, int j, int width, int height,
2638 const float uf = ((2.f * i) / width - 1.f);
2639 const float vf = ((2.f * j) / height - 1.f);
2640 const float rh = hypotf(uf, vf);
2641 const float sinzz = 1.f - rh * rh;
2642 const float h = 1.f + s->v_fov;
2643 const float sinz = (h - sqrtf(sinzz)) / (h / rh + rh / h);
2644 const float sinz2 = sinz * sinz;
2647 const float cosz = sqrtf(1.f - sinz2);
2649 const float theta = asinf(cosz);
2650 const float phi = atan2f(uf, vf);
2652 const float sin_phi = sinf(phi);
2653 const float cos_phi = cosf(phi);
2654 const float sin_theta = sinf(theta);
2655 const float cos_theta = cosf(theta);
2657 vec[0] = cos_theta * sin_phi;
2659 vec[2] = -cos_theta * cos_phi;
2667 normalize_vector(vec);
2672 * Calculate 3D coordinates on sphere for corresponding frame position in tetrahedron format.
2674 * @param s filter private context
2675 * @param i horizontal position on frame [0, width)
2676 * @param j vertical position on frame [0, height)
2677 * @param width frame width
2678 * @param height frame height
2679 * @param vec coordinates on sphere
2681 static int tetrahedron_to_xyz(const V360Context *s,
2682 int i, int j, int width, int height,
2685 const float uf = (float)i / width;
2686 const float vf = (float)j / height;
2688 vec[0] = uf < 0.5f ? uf * 4.f - 1.f : 3.f - uf * 4.f;
2689 vec[1] = 1.f - vf * 2.f;
2690 vec[2] = 2.f * fabsf(1.f - fabsf(1.f - uf * 2.f + vf)) - 1.f;
2692 normalize_vector(vec);
2698 * Calculate frame position in tetrahedron format for corresponding 3D coordinates on sphere.
2700 * @param s filter private context
2701 * @param vec coordinates on sphere
2702 * @param width frame width
2703 * @param height frame height
2704 * @param us horizontal coordinates for interpolation window
2705 * @param vs vertical coordinates for interpolation window
2706 * @param du horizontal relative coordinate
2707 * @param dv vertical relative coordinate
2709 static int xyz_to_tetrahedron(const V360Context *s,
2710 const float *vec, int width, int height,
2711 int16_t us[4][4], int16_t vs[4][4], float *du, float *dv)
2713 float d = 0.5f * (vec[0] * vec[0] + vec[1] * vec[1] + vec[2] * vec[2]);
2715 const float d0 = (vec[0] * 0.5f + vec[1] * 0.5f + vec[2] *-0.5f) / d;
2716 const float d1 = (vec[0] *-0.5f + vec[1] *-0.5f + vec[2] *-0.5f) / d;
2717 const float d2 = (vec[0] * 0.5f + vec[1] *-0.5f + vec[2] * 0.5f) / d;
2718 const float d3 = (vec[0] *-0.5f + vec[1] * 0.5f + vec[2] * 0.5f) / d;
2720 float uf, vf, x, y, z;
2723 d = FFMAX(d0, FFMAX3(d1, d2, d3));
2729 vf = 0.5f - y * 0.5f * s->input_mirror_modifier[1];
2731 if ((x + y >= 0.f && y + z >= 0.f && -z - x <= 0.f) ||
2732 (x + y <= 0.f && -y + z >= 0.f && z - x >= 0.f)) {
2733 uf = 0.25f * x * s->input_mirror_modifier[0] + 0.25f;
2735 uf = 0.75f - 0.25f * x * s->input_mirror_modifier[0];
2747 for (int i = -1; i < 3; i++) {
2748 for (int j = -1; j < 3; j++) {
2749 us[i + 1][j + 1] = mod(ui + j, width);
2750 vs[i + 1][j + 1] = av_clip(vi + i, 0, height - 1);
2758 * Calculate 3D coordinates on sphere for corresponding frame position in dual fisheye format.
2760 * @param s filter private context
2761 * @param i horizontal position on frame [0, width)
2762 * @param j vertical position on frame [0, height)
2763 * @param width frame width
2764 * @param height frame height
2765 * @param vec coordinates on sphere
2767 static int dfisheye_to_xyz(const V360Context *s,
2768 int i, int j, int width, int height,
2771 const float scale = 1.f + s->out_pad;
2773 const float ew = width / 2.f;
2774 const float eh = height;
2776 const int ei = i >= ew ? i - ew : i;
2777 const float m = i >= ew ? -1.f : 1.f;
2779 const float uf = ((2.f * ei) / ew - 1.f) * scale;
2780 const float vf = ((2.f * j) / eh - 1.f) * scale;
2782 const float h = hypotf(uf, vf);
2783 const float lh = h > 0.f ? h : 1.f;
2784 const float theta = m * M_PI_2 * (1.f - h);
2786 const float sin_theta = sinf(theta);
2787 const float cos_theta = cosf(theta);
2789 vec[0] = cos_theta * m * -uf / lh;
2790 vec[1] = cos_theta * -vf / lh;
2793 normalize_vector(vec);
2799 * Calculate frame position in dual fisheye format for corresponding 3D coordinates on sphere.
2801 * @param s filter private context
2802 * @param vec coordinates on sphere
2803 * @param width frame width
2804 * @param height frame height
2805 * @param us horizontal coordinates for interpolation window
2806 * @param vs vertical coordinates for interpolation window
2807 * @param du horizontal relative coordinate
2808 * @param dv vertical relative coordinate
2810 static int xyz_to_dfisheye(const V360Context *s,
2811 const float *vec, int width, int height,
2812 int16_t us[4][4], int16_t vs[4][4], float *du, float *dv)
2814 const float scale = 1.f - s->in_pad;
2816 const float ew = width / 2.f;
2817 const float eh = height;
2819 const float h = hypotf(vec[0], vec[1]);
2820 const float lh = h > 0.f ? h : 1.f;
2821 const float theta = acosf(fabsf(vec[2])) / M_PI;
2823 float uf = (theta * (-vec[0] / lh) * s->input_mirror_modifier[0] * scale + 0.5f) * ew;
2824 float vf = (theta * (-vec[1] / lh) * s->input_mirror_modifier[1] * scale + 0.5f) * eh;
2829 if (vec[2] >= 0.f) {
2832 u_shift = ceilf(ew);
2842 for (int i = -1; i < 3; i++) {
2843 for (int j = -1; j < 3; j++) {
2844 us[i + 1][j + 1] = av_clip(u_shift + ui + j, 0, width - 1);
2845 vs[i + 1][j + 1] = av_clip( vi + i, 0, height - 1);
2853 * Calculate 3D coordinates on sphere for corresponding frame position in barrel facebook's format.
2855 * @param s filter private context
2856 * @param i horizontal position on frame [0, width)
2857 * @param j vertical position on frame [0, height)
2858 * @param width frame width
2859 * @param height frame height
2860 * @param vec coordinates on sphere
2862 static int barrel_to_xyz(const V360Context *s,
2863 int i, int j, int width, int height,
2866 const float scale = 0.99f;
2867 float l_x, l_y, l_z;
2869 if (i < 4 * width / 5) {
2870 const float theta_range = M_PI_4;
2872 const int ew = 4 * width / 5;
2873 const int eh = height;
2875 const float phi = ((2.f * i) / ew - 1.f) * M_PI / scale;
2876 const float theta = ((2.f * j) / eh - 1.f) * theta_range / scale;
2878 const float sin_phi = sinf(phi);
2879 const float cos_phi = cosf(phi);
2880 const float sin_theta = sinf(theta);
2881 const float cos_theta = cosf(theta);
2883 l_x = cos_theta * sin_phi;
2885 l_z = -cos_theta * cos_phi;
2887 const int ew = width / 5;
2888 const int eh = height / 2;
2893 uf = 2.f * (i - 4 * ew) / ew - 1.f;
2894 vf = 2.f * (j ) / eh - 1.f;
2903 uf = 2.f * (i - 4 * ew) / ew - 1.f;
2904 vf = 2.f * (j - eh) / eh - 1.f;
2919 normalize_vector(vec);
2925 * Calculate frame position in barrel facebook's format for corresponding 3D coordinates on sphere.
2927 * @param s filter private context
2928 * @param vec coordinates on sphere
2929 * @param width frame width
2930 * @param height frame height
2931 * @param us horizontal coordinates for interpolation window
2932 * @param vs vertical coordinates for interpolation window
2933 * @param du horizontal relative coordinate
2934 * @param dv vertical relative coordinate
2936 static int xyz_to_barrel(const V360Context *s,
2937 const float *vec, int width, int height,
2938 int16_t us[4][4], int16_t vs[4][4], float *du, float *dv)
2940 const float scale = 0.99f;
2942 const float phi = atan2f(vec[0], -vec[2]) * s->input_mirror_modifier[0];
2943 const float theta = asinf(-vec[1]) * s->input_mirror_modifier[1];
2944 const float theta_range = M_PI_4;
2947 int u_shift, v_shift;
2951 if (theta > -theta_range && theta < theta_range) {
2955 u_shift = s->ih_flip ? width / 5 : 0;
2958 uf = (phi / M_PI * scale + 1.f) * ew / 2.f;
2959 vf = (theta / theta_range * scale + 1.f) * eh / 2.f;
2964 u_shift = s->ih_flip ? 0 : 4 * ew;
2966 if (theta < 0.f) { // UP
2967 uf = vec[0] / vec[1];
2968 vf = -vec[2] / vec[1];
2971 uf = -vec[0] / vec[1];
2972 vf = -vec[2] / vec[1];
2976 uf *= s->input_mirror_modifier[0] * s->input_mirror_modifier[1];
2977 vf *= s->input_mirror_modifier[1];
2979 uf = 0.5f * ew * (uf * scale + 1.f);
2980 vf = 0.5f * eh * (vf * scale + 1.f);
2989 for (int i = -1; i < 3; i++) {
2990 for (int j = -1; j < 3; j++) {
2991 us[i + 1][j + 1] = u_shift + av_clip(ui + j, 0, ew - 1);
2992 vs[i + 1][j + 1] = v_shift + av_clip(vi + i, 0, eh - 1);
2999 static void multiply_matrix(float c[3][3], const float a[3][3], const float b[3][3])
3001 for (int i = 0; i < 3; i++) {
3002 for (int j = 0; j < 3; j++) {
3005 for (int k = 0; k < 3; k++)
3006 sum += a[i][k] * b[k][j];
3014 * Calculate rotation matrix for yaw/pitch/roll angles.
3016 static inline void calculate_rotation_matrix(float yaw, float pitch, float roll,
3017 float rot_mat[3][3],
3018 const int rotation_order[3])
3020 const float yaw_rad = yaw * M_PI / 180.f;
3021 const float pitch_rad = pitch * M_PI / 180.f;
3022 const float roll_rad = roll * M_PI / 180.f;
3024 const float sin_yaw = sinf(-yaw_rad);
3025 const float cos_yaw = cosf(-yaw_rad);
3026 const float sin_pitch = sinf(pitch_rad);
3027 const float cos_pitch = cosf(pitch_rad);
3028 const float sin_roll = sinf(roll_rad);
3029 const float cos_roll = cosf(roll_rad);
3034 m[0][0][0] = cos_yaw; m[0][0][1] = 0; m[0][0][2] = sin_yaw;
3035 m[0][1][0] = 0; m[0][1][1] = 1; m[0][1][2] = 0;
3036 m[0][2][0] = -sin_yaw; m[0][2][1] = 0; m[0][2][2] = cos_yaw;
3038 m[1][0][0] = 1; m[1][0][1] = 0; m[1][0][2] = 0;
3039 m[1][1][0] = 0; m[1][1][1] = cos_pitch; m[1][1][2] = -sin_pitch;
3040 m[1][2][0] = 0; m[1][2][1] = sin_pitch; m[1][2][2] = cos_pitch;
3042 m[2][0][0] = cos_roll; m[2][0][1] = -sin_roll; m[2][0][2] = 0;
3043 m[2][1][0] = sin_roll; m[2][1][1] = cos_roll; m[2][1][2] = 0;
3044 m[2][2][0] = 0; m[2][2][1] = 0; m[2][2][2] = 1;
3046 multiply_matrix(temp, m[rotation_order[0]], m[rotation_order[1]]);
3047 multiply_matrix(rot_mat, temp, m[rotation_order[2]]);
3051 * Rotate vector with given rotation matrix.
3053 * @param rot_mat rotation matrix
3056 static inline void rotate(const float rot_mat[3][3],
3059 const float x_tmp = vec[0] * rot_mat[0][0] + vec[1] * rot_mat[0][1] + vec[2] * rot_mat[0][2];
3060 const float y_tmp = vec[0] * rot_mat[1][0] + vec[1] * rot_mat[1][1] + vec[2] * rot_mat[1][2];
3061 const float z_tmp = vec[0] * rot_mat[2][0] + vec[1] * rot_mat[2][1] + vec[2] * rot_mat[2][2];
3068 static inline void set_mirror_modifier(int h_flip, int v_flip, int d_flip,
3071 modifier[0] = h_flip ? -1.f : 1.f;
3072 modifier[1] = v_flip ? -1.f : 1.f;
3073 modifier[2] = d_flip ? -1.f : 1.f;
3076 static inline void mirror(const float *modifier, float *vec)
3078 vec[0] *= modifier[0];
3079 vec[1] *= modifier[1];
3080 vec[2] *= modifier[2];
3083 static int allocate_plane(V360Context *s, int sizeof_uv, int sizeof_ker, int sizeof_mask, int p)
3085 s->u[p] = av_calloc(s->uv_linesize[p] * s->pr_height[p], sizeof_uv);
3086 s->v[p] = av_calloc(s->uv_linesize[p] * s->pr_height[p], sizeof_uv);
3087 if (!s->u[p] || !s->v[p])
3088 return AVERROR(ENOMEM);
3090 s->ker[p] = av_calloc(s->uv_linesize[p] * s->pr_height[p], sizeof_ker);
3092 return AVERROR(ENOMEM);
3095 if (sizeof_mask && !p) {
3096 s->mask = av_calloc(s->pr_width[p] * s->pr_height[p], sizeof_mask);
3098 return AVERROR(ENOMEM);
3104 static void fov_from_dfov(int format, float d_fov, float w, float h, float *h_fov, float *v_fov)
3109 const float d = 0.5f * hypotf(w, h);
3111 *h_fov = d / h * d_fov;
3112 *v_fov = d / w * d_fov;
3118 const float da = tanf(0.5 * FFMIN(d_fov, 359.f) * M_PI / 180.f);
3119 const float d = hypotf(w, h);
3121 *h_fov = atan2f(da * w, d) * 360.f / M_PI;
3122 *v_fov = atan2f(da * h, d) * 360.f / M_PI;
3133 static void set_dimensions(int *outw, int *outh, int w, int h, const AVPixFmtDescriptor *desc)
3135 outw[1] = outw[2] = FF_CEIL_RSHIFT(w, desc->log2_chroma_w);
3136 outw[0] = outw[3] = w;
3137 outh[1] = outh[2] = FF_CEIL_RSHIFT(h, desc->log2_chroma_h);
3138 outh[0] = outh[3] = h;
3141 // Calculate remap data
3142 static av_always_inline int v360_slice(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
3144 V360Context *s = ctx->priv;
3146 for (int p = 0; p < s->nb_allocated; p++) {
3147 const int max_value = s->max_value;
3148 const int width = s->pr_width[p];
3149 const int uv_linesize = s->uv_linesize[p];
3150 const int height = s->pr_height[p];
3151 const int in_width = s->inplanewidth[p];
3152 const int in_height = s->inplaneheight[p];
3153 const int slice_start = (height * jobnr ) / nb_jobs;
3154 const int slice_end = (height * (jobnr + 1)) / nb_jobs;
3159 for (int j = slice_start; j < slice_end; j++) {
3160 for (int i = 0; i < width; i++) {
3161 int16_t *u = s->u[p] + (j * uv_linesize + i) * s->elements;
3162 int16_t *v = s->v[p] + (j * uv_linesize + i) * s->elements;
3163 int16_t *ker = s->ker[p] + (j * uv_linesize + i) * s->elements;
3164 uint8_t *mask8 = p ? NULL : s->mask + (j * s->pr_width[0] + i);
3165 uint16_t *mask16 = p ? NULL : (uint16_t *)s->mask + (j * s->pr_width[0] + i);
3166 int in_mask, out_mask;
3168 if (s->out_transpose)
3169 out_mask = s->out_transform(s, j, i, height, width, vec);
3171 out_mask = s->out_transform(s, i, j, width, height, vec);
3172 av_assert1(!isnan(vec[0]) && !isnan(vec[1]) && !isnan(vec[2]));
3173 rotate(s->rot_mat, vec);
3174 av_assert1(!isnan(vec[0]) && !isnan(vec[1]) && !isnan(vec[2]));
3175 normalize_vector(vec);
3176 mirror(s->output_mirror_modifier, vec);
3177 if (s->in_transpose)
3178 in_mask = s->in_transform(s, vec, in_height, in_width, rmap.v, rmap.u, &du, &dv);
3180 in_mask = s->in_transform(s, vec, in_width, in_height, rmap.u, rmap.v, &du, &dv);
3181 av_assert1(!isnan(du) && !isnan(dv));
3182 s->calculate_kernel(du, dv, &rmap, u, v, ker);
3184 if (!p && s->mask) {
3185 if (s->mask_size == 1) {
3186 mask8[0] = 255 * (out_mask & in_mask);
3188 mask16[0] = max_value * (out_mask & in_mask);
3198 static int config_output(AVFilterLink *outlink)
3200 AVFilterContext *ctx = outlink->src;
3201 AVFilterLink *inlink = ctx->inputs[0];
3202 V360Context *s = ctx->priv;
3203 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(inlink->format);
3204 const int depth = desc->comp[0].depth;
3205 const int sizeof_mask = s->mask_size = (depth + 7) >> 3;
3210 int in_offset_h, in_offset_w;
3211 int out_offset_h, out_offset_w;
3213 int (*prepare_out)(AVFilterContext *ctx);
3216 s->max_value = (1 << depth) - 1;
3217 s->input_mirror_modifier[0] = s->ih_flip ? -1.f : 1.f;
3218 s->input_mirror_modifier[1] = s->iv_flip ? -1.f : 1.f;
3220 switch (s->interp) {
3222 s->calculate_kernel = nearest_kernel;
3223 s->remap_slice = depth <= 8 ? remap1_8bit_slice : remap1_16bit_slice;
3225 sizeof_uv = sizeof(int16_t) * s->elements;
3229 s->calculate_kernel = bilinear_kernel;
3230 s->remap_slice = depth <= 8 ? remap2_8bit_slice : remap2_16bit_slice;
3231 s->elements = 2 * 2;
3232 sizeof_uv = sizeof(int16_t) * s->elements;
3233 sizeof_ker = sizeof(int16_t) * s->elements;
3236 s->calculate_kernel = bicubic_kernel;
3237 s->remap_slice = depth <= 8 ? remap4_8bit_slice : remap4_16bit_slice;
3238 s->elements = 4 * 4;
3239 sizeof_uv = sizeof(int16_t) * s->elements;
3240 sizeof_ker = sizeof(int16_t) * s->elements;
3243 s->calculate_kernel = lanczos_kernel;
3244 s->remap_slice = depth <= 8 ? remap4_8bit_slice : remap4_16bit_slice;
3245 s->elements = 4 * 4;
3246 sizeof_uv = sizeof(int16_t) * s->elements;
3247 sizeof_ker = sizeof(int16_t) * s->elements;
3250 s->calculate_kernel = spline16_kernel;
3251 s->remap_slice = depth <= 8 ? remap4_8bit_slice : remap4_16bit_slice;
3252 s->elements = 4 * 4;
3253 sizeof_uv = sizeof(int16_t) * s->elements;
3254 sizeof_ker = sizeof(int16_t) * s->elements;
3257 s->calculate_kernel = gaussian_kernel;
3258 s->remap_slice = depth <= 8 ? remap4_8bit_slice : remap4_16bit_slice;
3259 s->elements = 4 * 4;
3260 sizeof_uv = sizeof(int16_t) * s->elements;
3261 sizeof_ker = sizeof(int16_t) * s->elements;
3267 ff_v360_init(s, depth);
3269 for (int order = 0; order < NB_RORDERS; order++) {
3270 const char c = s->rorder[order];
3274 av_log(ctx, AV_LOG_ERROR,
3275 "Incomplete rorder option. Direction for all 3 rotation orders should be specified.\n");
3276 return AVERROR(EINVAL);
3279 rorder = get_rorder(c);
3281 av_log(ctx, AV_LOG_ERROR,
3282 "Incorrect rotation order symbol '%c' in rorder option.\n", c);
3283 return AVERROR(EINVAL);
3286 s->rotation_order[order] = rorder;
3289 switch (s->in_stereo) {
3293 in_offset_w = in_offset_h = 0;
3311 set_dimensions(s->inplanewidth, s->inplaneheight, w, h, desc);
3312 set_dimensions(s->in_offset_w, s->in_offset_h, in_offset_w, in_offset_h, desc);
3314 s->in_width = s->inplanewidth[0];
3315 s->in_height = s->inplaneheight[0];
3317 if (s->id_fov > 0.f)
3318 fov_from_dfov(s->in, s->id_fov, w, h, &s->ih_fov, &s->iv_fov);
3320 if (s->in_transpose)
3321 FFSWAP(int, s->in_width, s->in_height);
3324 case EQUIRECTANGULAR:
3325 s->in_transform = xyz_to_equirect;
3331 s->in_transform = xyz_to_cube3x2;
3332 err = prepare_cube_in(ctx);
3337 s->in_transform = xyz_to_cube1x6;
3338 err = prepare_cube_in(ctx);
3343 s->in_transform = xyz_to_cube6x1;
3344 err = prepare_cube_in(ctx);
3349 s->in_transform = xyz_to_eac;
3350 err = prepare_eac_in(ctx);
3355 s->in_transform = xyz_to_flat;
3356 err = prepare_flat_in(ctx);
3362 av_log(ctx, AV_LOG_ERROR, "Supplied format is not accepted as input.\n");
3363 return AVERROR(EINVAL);
3365 s->in_transform = xyz_to_dfisheye;
3371 s->in_transform = xyz_to_barrel;
3377 s->in_transform = xyz_to_stereographic;
3378 err = prepare_stereographic_in(ctx);
3383 s->in_transform = xyz_to_mercator;
3389 s->in_transform = xyz_to_ball;
3395 s->in_transform = xyz_to_hammer;
3401 s->in_transform = xyz_to_sinusoidal;
3407 s->in_transform = xyz_to_fisheye;
3408 err = prepare_fisheye_in(ctx);
3413 s->in_transform = xyz_to_cylindrical;
3414 err = prepare_cylindrical_in(ctx);
3419 s->in_transform = xyz_to_tetrahedron;
3425 av_log(ctx, AV_LOG_ERROR, "Specified input format is not handled.\n");
3434 case EQUIRECTANGULAR:
3435 s->out_transform = equirect_to_xyz;
3441 s->out_transform = cube3x2_to_xyz;
3442 prepare_out = prepare_cube_out;
3443 w = lrintf(wf / 4.f * 3.f);
3447 s->out_transform = cube1x6_to_xyz;
3448 prepare_out = prepare_cube_out;
3449 w = lrintf(wf / 4.f);
3450 h = lrintf(hf * 3.f);
3453 s->out_transform = cube6x1_to_xyz;
3454 prepare_out = prepare_cube_out;
3455 w = lrintf(wf / 2.f * 3.f);
3456 h = lrintf(hf / 2.f);
3459 s->out_transform = eac_to_xyz;
3460 prepare_out = prepare_eac_out;
3462 h = lrintf(hf / 8.f * 9.f);
3465 s->out_transform = flat_to_xyz;
3466 prepare_out = prepare_flat_out;
3471 s->out_transform = dfisheye_to_xyz;
3477 s->out_transform = barrel_to_xyz;
3479 w = lrintf(wf / 4.f * 5.f);
3483 s->out_transform = stereographic_to_xyz;
3484 prepare_out = prepare_stereographic_out;
3486 h = lrintf(hf * 2.f);
3489 s->out_transform = mercator_to_xyz;
3492 h = lrintf(hf * 2.f);
3495 s->out_transform = ball_to_xyz;
3498 h = lrintf(hf * 2.f);
3501 s->out_transform = hammer_to_xyz;
3507 s->out_transform = sinusoidal_to_xyz;
3513 s->out_transform = fisheye_to_xyz;
3514 prepare_out = prepare_fisheye_out;
3515 w = lrintf(wf * 0.5f);
3519 s->out_transform = pannini_to_xyz;
3525 s->out_transform = cylindrical_to_xyz;
3526 prepare_out = prepare_cylindrical_out;
3528 h = lrintf(hf * 0.5f);
3531 s->out_transform = perspective_to_xyz;
3533 w = lrintf(wf / 2.f);
3537 s->out_transform = tetrahedron_to_xyz;
3543 av_log(ctx, AV_LOG_ERROR, "Specified output format is not handled.\n");
3547 // Override resolution with user values if specified
3548 if (s->width > 0 && s->height > 0) {
3551 } else if (s->width > 0 || s->height > 0) {
3552 av_log(ctx, AV_LOG_ERROR, "Both width and height values should be specified.\n");
3553 return AVERROR(EINVAL);
3555 if (s->out_transpose)
3558 if (s->in_transpose)
3563 fov_from_dfov(s->out, s->d_fov, w, h, &s->h_fov, &s->v_fov);
3566 err = prepare_out(ctx);
3571 set_dimensions(s->pr_width, s->pr_height, w, h, desc);
3573 s->out_width = s->pr_width[0];
3574 s->out_height = s->pr_height[0];
3576 if (s->out_transpose)
3577 FFSWAP(int, s->out_width, s->out_height);
3579 switch (s->out_stereo) {
3581 out_offset_w = out_offset_h = 0;
3597 set_dimensions(s->out_offset_w, s->out_offset_h, out_offset_w, out_offset_h, desc);
3598 set_dimensions(s->planewidth, s->planeheight, w, h, desc);
3600 for (int i = 0; i < 4; i++)
3601 s->uv_linesize[i] = FFALIGN(s->pr_width[i], 8);
3606 s->nb_planes = av_pix_fmt_count_planes(inlink->format);
3607 have_alpha = !!(desc->flags & AV_PIX_FMT_FLAG_ALPHA);
3609 if (desc->log2_chroma_h == desc->log2_chroma_w && desc->log2_chroma_h == 0) {
3610 s->nb_allocated = 1;
3611 s->map[0] = s->map[1] = s->map[2] = s->map[3] = 0;
3613 s->nb_allocated = 2;
3614 s->map[0] = s->map[3] = 0;
3615 s->map[1] = s->map[2] = 1;
3618 for (int i = 0; i < s->nb_allocated; i++)
3619 allocate_plane(s, sizeof_uv, sizeof_ker, sizeof_mask * have_alpha * s->alpha, i);
3621 calculate_rotation_matrix(s->yaw, s->pitch, s->roll, s->rot_mat, s->rotation_order);
3622 set_mirror_modifier(s->h_flip, s->v_flip, s->d_flip, s->output_mirror_modifier);
3624 ctx->internal->execute(ctx, v360_slice, NULL, NULL, FFMIN(outlink->h, ff_filter_get_nb_threads(ctx)));
3629 static int filter_frame(AVFilterLink *inlink, AVFrame *in)
3631 AVFilterContext *ctx = inlink->dst;
3632 AVFilterLink *outlink = ctx->outputs[0];
3633 V360Context *s = ctx->priv;
3637 out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
3640 return AVERROR(ENOMEM);
3642 av_frame_copy_props(out, in);
3647 ctx->internal->execute(ctx, s->remap_slice, &td, NULL, FFMIN(outlink->h, ff_filter_get_nb_threads(ctx)));
3650 return ff_filter_frame(outlink, out);
3653 static av_cold void uninit(AVFilterContext *ctx)
3655 V360Context *s = ctx->priv;
3657 for (int p = 0; p < s->nb_allocated; p++) {
3660 av_freep(&s->ker[p]);
3665 static const AVFilterPad inputs[] = {
3668 .type = AVMEDIA_TYPE_VIDEO,
3669 .filter_frame = filter_frame,
3674 static const AVFilterPad outputs[] = {
3677 .type = AVMEDIA_TYPE_VIDEO,
3678 .config_props = config_output,
3683 AVFilter ff_vf_v360 = {
3685 .description = NULL_IF_CONFIG_SMALL("Convert 360 projection of video."),
3686 .priv_size = sizeof(V360Context),
3688 .query_formats = query_formats,
3691 .priv_class = &v360_class,
3692 .flags = AVFILTER_FLAG_SLICE_THREADS,