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 { "tsp", "truncated square pyramid", 0, AV_OPT_TYPE_CONST, {.i64=TSPYRAMID}, 0, 0, FLAGS, "in" },
81 { "hequirect", "half equirectangular", 0, AV_OPT_TYPE_CONST, {.i64=HEQUIRECTANGULAR},0, 0, FLAGS, "in" },
82 { "he", "half equirectangular", 0, AV_OPT_TYPE_CONST, {.i64=HEQUIRECTANGULAR},0, 0, FLAGS, "in" },
83 { "output", "set output projection", OFFSET(out), AV_OPT_TYPE_INT, {.i64=CUBEMAP_3_2}, 0, NB_PROJECTIONS-1, FLAGS, "out" },
84 { "e", "equirectangular", 0, AV_OPT_TYPE_CONST, {.i64=EQUIRECTANGULAR}, 0, 0, FLAGS, "out" },
85 { "equirect", "equirectangular", 0, AV_OPT_TYPE_CONST, {.i64=EQUIRECTANGULAR}, 0, 0, FLAGS, "out" },
86 { "c3x2", "cubemap 3x2", 0, AV_OPT_TYPE_CONST, {.i64=CUBEMAP_3_2}, 0, 0, FLAGS, "out" },
87 { "c6x1", "cubemap 6x1", 0, AV_OPT_TYPE_CONST, {.i64=CUBEMAP_6_1}, 0, 0, FLAGS, "out" },
88 { "eac", "equi-angular cubemap", 0, AV_OPT_TYPE_CONST, {.i64=EQUIANGULAR}, 0, 0, FLAGS, "out" },
89 { "dfisheye", "dual fisheye", 0, AV_OPT_TYPE_CONST, {.i64=DUAL_FISHEYE}, 0, 0, FLAGS, "out" },
90 { "flat", "regular video", 0, AV_OPT_TYPE_CONST, {.i64=FLAT}, 0, 0, FLAGS, "out" },
91 {"rectilinear", "regular video", 0, AV_OPT_TYPE_CONST, {.i64=FLAT}, 0, 0, FLAGS, "out" },
92 { "gnomonic", "regular video", 0, AV_OPT_TYPE_CONST, {.i64=FLAT}, 0, 0, FLAGS, "out" },
93 { "barrel", "barrel facebook's 360 format", 0, AV_OPT_TYPE_CONST, {.i64=BARREL}, 0, 0, FLAGS, "out" },
94 { "fb", "barrel facebook's 360 format", 0, AV_OPT_TYPE_CONST, {.i64=BARREL}, 0, 0, FLAGS, "out" },
95 { "c1x6", "cubemap 1x6", 0, AV_OPT_TYPE_CONST, {.i64=CUBEMAP_1_6}, 0, 0, FLAGS, "out" },
96 { "sg", "stereographic", 0, AV_OPT_TYPE_CONST, {.i64=STEREOGRAPHIC}, 0, 0, FLAGS, "out" },
97 { "mercator", "mercator", 0, AV_OPT_TYPE_CONST, {.i64=MERCATOR}, 0, 0, FLAGS, "out" },
98 { "ball", "ball", 0, AV_OPT_TYPE_CONST, {.i64=BALL}, 0, 0, FLAGS, "out" },
99 { "hammer", "hammer", 0, AV_OPT_TYPE_CONST, {.i64=HAMMER}, 0, 0, FLAGS, "out" },
100 {"sinusoidal", "sinusoidal", 0, AV_OPT_TYPE_CONST, {.i64=SINUSOIDAL}, 0, 0, FLAGS, "out" },
101 { "fisheye", "fisheye", 0, AV_OPT_TYPE_CONST, {.i64=FISHEYE}, 0, 0, FLAGS, "out" },
102 { "pannini", "pannini", 0, AV_OPT_TYPE_CONST, {.i64=PANNINI}, 0, 0, FLAGS, "out" },
103 {"cylindrical", "cylindrical", 0, AV_OPT_TYPE_CONST, {.i64=CYLINDRICAL}, 0, 0, FLAGS, "out" },
104 {"perspective", "perspective", 0, AV_OPT_TYPE_CONST, {.i64=PERSPECTIVE}, 0, 0, FLAGS, "out" },
105 {"tetrahedron", "tetrahedron", 0, AV_OPT_TYPE_CONST, {.i64=TETRAHEDRON}, 0, 0, FLAGS, "out" },
106 {"barrelsplit", "barrel split facebook's 360 format", 0, AV_OPT_TYPE_CONST, {.i64=BARREL_SPLIT}, 0, 0, FLAGS, "out" },
107 { "tsp", "truncated square pyramid", 0, AV_OPT_TYPE_CONST, {.i64=TSPYRAMID}, 0, 0, FLAGS, "out" },
108 { "hequirect", "half equirectangular", 0, AV_OPT_TYPE_CONST, {.i64=HEQUIRECTANGULAR},0, 0, FLAGS, "out" },
109 { "he", "half equirectangular", 0, AV_OPT_TYPE_CONST, {.i64=HEQUIRECTANGULAR},0, 0, FLAGS, "out" },
110 { "interp", "set interpolation method", OFFSET(interp), AV_OPT_TYPE_INT, {.i64=BILINEAR}, 0, NB_INTERP_METHODS-1, FLAGS, "interp" },
111 { "near", "nearest neighbour", 0, AV_OPT_TYPE_CONST, {.i64=NEAREST}, 0, 0, FLAGS, "interp" },
112 { "nearest", "nearest neighbour", 0, AV_OPT_TYPE_CONST, {.i64=NEAREST}, 0, 0, FLAGS, "interp" },
113 { "line", "bilinear interpolation", 0, AV_OPT_TYPE_CONST, {.i64=BILINEAR}, 0, 0, FLAGS, "interp" },
114 { "linear", "bilinear interpolation", 0, AV_OPT_TYPE_CONST, {.i64=BILINEAR}, 0, 0, FLAGS, "interp" },
115 { "cube", "bicubic interpolation", 0, AV_OPT_TYPE_CONST, {.i64=BICUBIC}, 0, 0, FLAGS, "interp" },
116 { "cubic", "bicubic interpolation", 0, AV_OPT_TYPE_CONST, {.i64=BICUBIC}, 0, 0, FLAGS, "interp" },
117 { "lanc", "lanczos interpolation", 0, AV_OPT_TYPE_CONST, {.i64=LANCZOS}, 0, 0, FLAGS, "interp" },
118 { "lanczos", "lanczos interpolation", 0, AV_OPT_TYPE_CONST, {.i64=LANCZOS}, 0, 0, FLAGS, "interp" },
119 { "sp16", "spline16 interpolation", 0, AV_OPT_TYPE_CONST, {.i64=SPLINE16}, 0, 0, FLAGS, "interp" },
120 { "spline16", "spline16 interpolation", 0, AV_OPT_TYPE_CONST, {.i64=SPLINE16}, 0, 0, FLAGS, "interp" },
121 { "gauss", "gaussian interpolation", 0, AV_OPT_TYPE_CONST, {.i64=GAUSSIAN}, 0, 0, FLAGS, "interp" },
122 { "gaussian", "gaussian interpolation", 0, AV_OPT_TYPE_CONST, {.i64=GAUSSIAN}, 0, 0, FLAGS, "interp" },
123 { "w", "output width", OFFSET(width), AV_OPT_TYPE_INT, {.i64=0}, 0, INT16_MAX, FLAGS, "w"},
124 { "h", "output height", OFFSET(height), AV_OPT_TYPE_INT, {.i64=0}, 0, INT16_MAX, FLAGS, "h"},
125 { "in_stereo", "input stereo format", OFFSET(in_stereo), AV_OPT_TYPE_INT, {.i64=STEREO_2D}, 0, NB_STEREO_FMTS-1, FLAGS, "stereo" },
126 {"out_stereo", "output stereo format", OFFSET(out_stereo), AV_OPT_TYPE_INT, {.i64=STEREO_2D}, 0, NB_STEREO_FMTS-1, FLAGS, "stereo" },
127 { "2d", "2d mono", 0, AV_OPT_TYPE_CONST, {.i64=STEREO_2D}, 0, 0, FLAGS, "stereo" },
128 { "sbs", "side by side", 0, AV_OPT_TYPE_CONST, {.i64=STEREO_SBS}, 0, 0, FLAGS, "stereo" },
129 { "tb", "top bottom", 0, AV_OPT_TYPE_CONST, {.i64=STEREO_TB}, 0, 0, FLAGS, "stereo" },
130 { "in_forder", "input cubemap face order", OFFSET(in_forder), AV_OPT_TYPE_STRING, {.str="rludfb"}, 0, NB_DIRECTIONS-1, FLAGS, "in_forder"},
131 {"out_forder", "output cubemap face order", OFFSET(out_forder), AV_OPT_TYPE_STRING, {.str="rludfb"}, 0, NB_DIRECTIONS-1, FLAGS, "out_forder"},
132 { "in_frot", "input cubemap face rotation", OFFSET(in_frot), AV_OPT_TYPE_STRING, {.str="000000"}, 0, NB_DIRECTIONS-1, FLAGS, "in_frot"},
133 { "out_frot", "output cubemap face rotation",OFFSET(out_frot), AV_OPT_TYPE_STRING, {.str="000000"}, 0, NB_DIRECTIONS-1, FLAGS, "out_frot"},
134 { "in_pad", "percent input cubemap pads", OFFSET(in_pad), AV_OPT_TYPE_FLOAT, {.dbl=0.f}, 0.f, 1.f,TFLAGS, "in_pad"},
135 { "out_pad", "percent output cubemap pads", OFFSET(out_pad), AV_OPT_TYPE_FLOAT, {.dbl=0.f}, 0.f, 1.f,TFLAGS, "out_pad"},
136 { "fin_pad", "fixed input cubemap pads", OFFSET(fin_pad), AV_OPT_TYPE_INT, {.i64=0}, 0, 100,TFLAGS, "fin_pad"},
137 { "fout_pad", "fixed output cubemap pads", OFFSET(fout_pad), AV_OPT_TYPE_INT, {.i64=0}, 0, 100,TFLAGS, "fout_pad"},
138 { "yaw", "yaw rotation", OFFSET(yaw), AV_OPT_TYPE_FLOAT, {.dbl=0.f}, -180.f, 180.f,TFLAGS, "yaw"},
139 { "pitch", "pitch rotation", OFFSET(pitch), AV_OPT_TYPE_FLOAT, {.dbl=0.f}, -180.f, 180.f,TFLAGS, "pitch"},
140 { "roll", "roll rotation", OFFSET(roll), AV_OPT_TYPE_FLOAT, {.dbl=0.f}, -180.f, 180.f,TFLAGS, "roll"},
141 { "rorder", "rotation order", OFFSET(rorder), AV_OPT_TYPE_STRING, {.str="ypr"}, 0, 0,TFLAGS, "rorder"},
142 { "h_fov", "horizontal field of view", OFFSET(h_fov), AV_OPT_TYPE_FLOAT, {.dbl=90.f}, 0.00001f, 360.f,TFLAGS, "h_fov"},
143 { "v_fov", "vertical field of view", OFFSET(v_fov), AV_OPT_TYPE_FLOAT, {.dbl=45.f}, 0.00001f, 360.f,TFLAGS, "v_fov"},
144 { "d_fov", "diagonal field of view", OFFSET(d_fov), AV_OPT_TYPE_FLOAT, {.dbl=0.f}, 0.f, 360.f,TFLAGS, "d_fov"},
145 { "h_flip", "flip out video horizontally", OFFSET(h_flip), AV_OPT_TYPE_BOOL, {.i64=0}, 0, 1,TFLAGS, "h_flip"},
146 { "v_flip", "flip out video vertically", OFFSET(v_flip), AV_OPT_TYPE_BOOL, {.i64=0}, 0, 1,TFLAGS, "v_flip"},
147 { "d_flip", "flip out video indepth", OFFSET(d_flip), AV_OPT_TYPE_BOOL, {.i64=0}, 0, 1,TFLAGS, "d_flip"},
148 { "ih_flip", "flip in video horizontally", OFFSET(ih_flip), AV_OPT_TYPE_BOOL, {.i64=0}, 0, 1,TFLAGS, "ih_flip"},
149 { "iv_flip", "flip in video vertically", OFFSET(iv_flip), AV_OPT_TYPE_BOOL, {.i64=0}, 0, 1,TFLAGS, "iv_flip"},
150 { "in_trans", "transpose video input", OFFSET(in_transpose), AV_OPT_TYPE_BOOL, {.i64=0}, 0, 1, FLAGS, "in_transpose"},
151 { "out_trans", "transpose video output", OFFSET(out_transpose), AV_OPT_TYPE_BOOL, {.i64=0}, 0, 1, FLAGS, "out_transpose"},
152 { "ih_fov", "input horizontal field of view",OFFSET(ih_fov), AV_OPT_TYPE_FLOAT, {.dbl=90.f}, 0.00001f, 360.f,TFLAGS, "ih_fov"},
153 { "iv_fov", "input vertical field of view", OFFSET(iv_fov), AV_OPT_TYPE_FLOAT, {.dbl=45.f}, 0.00001f, 360.f,TFLAGS, "iv_fov"},
154 { "id_fov", "input diagonal field of view", OFFSET(id_fov), AV_OPT_TYPE_FLOAT, {.dbl=0.f}, 0.f, 360.f,TFLAGS, "id_fov"},
155 {"alpha_mask", "build mask in alpha plane", OFFSET(alpha), AV_OPT_TYPE_BOOL, {.i64=0}, 0, 1, FLAGS, "alpha"},
159 AVFILTER_DEFINE_CLASS(v360);
161 static int query_formats(AVFilterContext *ctx)
163 V360Context *s = ctx->priv;
164 static const enum AVPixelFormat pix_fmts[] = {
166 AV_PIX_FMT_YUVA444P, AV_PIX_FMT_YUVA444P9,
167 AV_PIX_FMT_YUVA444P10, AV_PIX_FMT_YUVA444P12,
168 AV_PIX_FMT_YUVA444P16,
171 AV_PIX_FMT_YUVA422P, AV_PIX_FMT_YUVA422P9,
172 AV_PIX_FMT_YUVA422P10, AV_PIX_FMT_YUVA422P12,
173 AV_PIX_FMT_YUVA422P16,
176 AV_PIX_FMT_YUVA420P, AV_PIX_FMT_YUVA420P9,
177 AV_PIX_FMT_YUVA420P10, AV_PIX_FMT_YUVA420P16,
180 AV_PIX_FMT_YUVJ444P, AV_PIX_FMT_YUVJ440P,
181 AV_PIX_FMT_YUVJ422P, AV_PIX_FMT_YUVJ420P,
185 AV_PIX_FMT_YUV444P, AV_PIX_FMT_YUV444P9,
186 AV_PIX_FMT_YUV444P10, AV_PIX_FMT_YUV444P12,
187 AV_PIX_FMT_YUV444P14, AV_PIX_FMT_YUV444P16,
190 AV_PIX_FMT_YUV440P, AV_PIX_FMT_YUV440P10,
191 AV_PIX_FMT_YUV440P12,
194 AV_PIX_FMT_YUV422P, AV_PIX_FMT_YUV422P9,
195 AV_PIX_FMT_YUV422P10, AV_PIX_FMT_YUV422P12,
196 AV_PIX_FMT_YUV422P14, AV_PIX_FMT_YUV422P16,
199 AV_PIX_FMT_YUV420P, AV_PIX_FMT_YUV420P9,
200 AV_PIX_FMT_YUV420P10, AV_PIX_FMT_YUV420P12,
201 AV_PIX_FMT_YUV420P14, AV_PIX_FMT_YUV420P16,
210 AV_PIX_FMT_GBRP, AV_PIX_FMT_GBRP9,
211 AV_PIX_FMT_GBRP10, AV_PIX_FMT_GBRP12,
212 AV_PIX_FMT_GBRP14, AV_PIX_FMT_GBRP16,
215 AV_PIX_FMT_GBRAP, AV_PIX_FMT_GBRAP10,
216 AV_PIX_FMT_GBRAP12, AV_PIX_FMT_GBRAP16,
219 AV_PIX_FMT_GRAY8, AV_PIX_FMT_GRAY9,
220 AV_PIX_FMT_GRAY10, AV_PIX_FMT_GRAY12,
221 AV_PIX_FMT_GRAY14, AV_PIX_FMT_GRAY16,
225 static const enum AVPixelFormat alpha_pix_fmts[] = {
226 AV_PIX_FMT_YUVA444P, AV_PIX_FMT_YUVA444P9,
227 AV_PIX_FMT_YUVA444P10, AV_PIX_FMT_YUVA444P12,
228 AV_PIX_FMT_YUVA444P16,
229 AV_PIX_FMT_YUVA422P, AV_PIX_FMT_YUVA422P9,
230 AV_PIX_FMT_YUVA422P10, AV_PIX_FMT_YUVA422P12,
231 AV_PIX_FMT_YUVA422P16,
232 AV_PIX_FMT_YUVA420P, AV_PIX_FMT_YUVA420P9,
233 AV_PIX_FMT_YUVA420P10, AV_PIX_FMT_YUVA420P16,
234 AV_PIX_FMT_GBRAP, AV_PIX_FMT_GBRAP10,
235 AV_PIX_FMT_GBRAP12, AV_PIX_FMT_GBRAP16,
239 AVFilterFormats *fmts_list = ff_make_format_list(s->alpha ? alpha_pix_fmts : pix_fmts);
241 return AVERROR(ENOMEM);
242 return ff_set_common_formats(ctx, fmts_list);
245 #define DEFINE_REMAP1_LINE(bits, div) \
246 static void remap1_##bits##bit_line_c(uint8_t *dst, int width, const uint8_t *const src, \
247 ptrdiff_t in_linesize, \
248 const int16_t *const u, const int16_t *const v, \
249 const int16_t *const ker) \
251 const uint##bits##_t *const s = (const uint##bits##_t *const)src; \
252 uint##bits##_t *d = (uint##bits##_t *)dst; \
254 in_linesize /= div; \
256 for (int x = 0; x < width; x++) \
257 d[x] = s[v[x] * in_linesize + u[x]]; \
260 DEFINE_REMAP1_LINE( 8, 1)
261 DEFINE_REMAP1_LINE(16, 2)
264 * Generate remapping function with a given window size and pixel depth.
266 * @param ws size of interpolation window
267 * @param bits number of bits per pixel
269 #define DEFINE_REMAP(ws, bits) \
270 static int remap##ws##_##bits##bit_slice(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs) \
272 ThreadData *td = arg; \
273 const V360Context *s = ctx->priv; \
274 const AVFrame *in = td->in; \
275 AVFrame *out = td->out; \
277 for (int stereo = 0; stereo < 1 + s->out_stereo > STEREO_2D; stereo++) { \
278 for (int plane = 0; plane < s->nb_planes; plane++) { \
279 const unsigned map = s->map[plane]; \
280 const int in_linesize = in->linesize[plane]; \
281 const int out_linesize = out->linesize[plane]; \
282 const int uv_linesize = s->uv_linesize[plane]; \
283 const int in_offset_w = stereo ? s->in_offset_w[plane] : 0; \
284 const int in_offset_h = stereo ? s->in_offset_h[plane] : 0; \
285 const int out_offset_w = stereo ? s->out_offset_w[plane] : 0; \
286 const int out_offset_h = stereo ? s->out_offset_h[plane] : 0; \
287 const uint8_t *const src = in->data[plane] + \
288 in_offset_h * in_linesize + in_offset_w * (bits >> 3); \
289 uint8_t *dst = out->data[plane] + out_offset_h * out_linesize + out_offset_w * (bits >> 3); \
290 const uint8_t *mask = plane == 3 ? s->mask : NULL; \
291 const int width = s->pr_width[plane]; \
292 const int height = s->pr_height[plane]; \
294 const int slice_start = (height * jobnr ) / nb_jobs; \
295 const int slice_end = (height * (jobnr + 1)) / nb_jobs; \
297 for (int y = slice_start; y < slice_end && !mask; y++) { \
298 const int16_t *const u = s->u[map] + y * uv_linesize * ws * ws; \
299 const int16_t *const v = s->v[map] + y * uv_linesize * ws * ws; \
300 const int16_t *const ker = s->ker[map] + y * uv_linesize * ws * ws; \
302 s->remap_line(dst + y * out_linesize, width, src, in_linesize, u, v, ker); \
305 for (int y = slice_start; y < slice_end && mask; y++) { \
306 memcpy(dst + y * out_linesize, mask + y * width * (bits >> 3), width * (bits >> 3)); \
321 #define DEFINE_REMAP_LINE(ws, bits, div) \
322 static void remap##ws##_##bits##bit_line_c(uint8_t *dst, int width, const uint8_t *const src, \
323 ptrdiff_t in_linesize, \
324 const int16_t *const u, const int16_t *const v, \
325 const int16_t *const ker) \
327 const uint##bits##_t *const s = (const uint##bits##_t *const)src; \
328 uint##bits##_t *d = (uint##bits##_t *)dst; \
330 in_linesize /= div; \
332 for (int x = 0; x < width; x++) { \
333 const int16_t *const uu = u + x * ws * ws; \
334 const int16_t *const vv = v + x * ws * ws; \
335 const int16_t *const kker = ker + x * ws * ws; \
338 for (int i = 0; i < ws; i++) { \
339 for (int j = 0; j < ws; j++) { \
340 tmp += kker[i * ws + j] * s[vv[i * ws + j] * in_linesize + uu[i * ws + j]]; \
344 d[x] = av_clip_uint##bits(tmp >> 14); \
348 DEFINE_REMAP_LINE(2, 8, 1)
349 DEFINE_REMAP_LINE(4, 8, 1)
350 DEFINE_REMAP_LINE(2, 16, 2)
351 DEFINE_REMAP_LINE(4, 16, 2)
353 void ff_v360_init(V360Context *s, int depth)
357 s->remap_line = depth <= 8 ? remap1_8bit_line_c : remap1_16bit_line_c;
360 s->remap_line = depth <= 8 ? remap2_8bit_line_c : remap2_16bit_line_c;
366 s->remap_line = depth <= 8 ? remap4_8bit_line_c : remap4_16bit_line_c;
371 ff_v360_init_x86(s, depth);
375 * Save nearest pixel coordinates for remapping.
377 * @param du horizontal relative coordinate
378 * @param dv vertical relative coordinate
379 * @param rmap calculated 4x4 window
380 * @param u u remap data
381 * @param v v remap data
382 * @param ker ker remap data
384 static void nearest_kernel(float du, float dv, const XYRemap *rmap,
385 int16_t *u, int16_t *v, int16_t *ker)
387 const int i = lrintf(dv) + 1;
388 const int j = lrintf(du) + 1;
390 u[0] = rmap->u[i][j];
391 v[0] = rmap->v[i][j];
395 * Calculate kernel for bilinear interpolation.
397 * @param du horizontal relative coordinate
398 * @param dv vertical relative coordinate
399 * @param rmap calculated 4x4 window
400 * @param u u remap data
401 * @param v v remap data
402 * @param ker ker remap data
404 static void bilinear_kernel(float du, float dv, const XYRemap *rmap,
405 int16_t *u, int16_t *v, int16_t *ker)
407 for (int i = 0; i < 2; i++) {
408 for (int j = 0; j < 2; j++) {
409 u[i * 2 + j] = rmap->u[i + 1][j + 1];
410 v[i * 2 + j] = rmap->v[i + 1][j + 1];
414 ker[0] = lrintf((1.f - du) * (1.f - dv) * 16385.f);
415 ker[1] = lrintf( du * (1.f - dv) * 16385.f);
416 ker[2] = lrintf((1.f - du) * dv * 16385.f);
417 ker[3] = lrintf( du * dv * 16385.f);
421 * Calculate 1-dimensional cubic coefficients.
423 * @param t relative coordinate
424 * @param coeffs coefficients
426 static inline void calculate_bicubic_coeffs(float t, float *coeffs)
428 const float tt = t * t;
429 const float ttt = t * t * t;
431 coeffs[0] = - t / 3.f + tt / 2.f - ttt / 6.f;
432 coeffs[1] = 1.f - t / 2.f - tt + ttt / 2.f;
433 coeffs[2] = t + tt / 2.f - ttt / 2.f;
434 coeffs[3] = - t / 6.f + ttt / 6.f;
438 * Calculate kernel for bicubic interpolation.
440 * @param du horizontal relative coordinate
441 * @param dv vertical relative coordinate
442 * @param rmap calculated 4x4 window
443 * @param u u remap data
444 * @param v v remap data
445 * @param ker ker remap data
447 static void bicubic_kernel(float du, float dv, const XYRemap *rmap,
448 int16_t *u, int16_t *v, int16_t *ker)
453 calculate_bicubic_coeffs(du, du_coeffs);
454 calculate_bicubic_coeffs(dv, dv_coeffs);
456 for (int i = 0; i < 4; i++) {
457 for (int j = 0; j < 4; j++) {
458 u[i * 4 + j] = rmap->u[i][j];
459 v[i * 4 + j] = rmap->v[i][j];
460 ker[i * 4 + j] = lrintf(du_coeffs[j] * dv_coeffs[i] * 16385.f);
466 * Calculate 1-dimensional lanczos coefficients.
468 * @param t relative coordinate
469 * @param coeffs coefficients
471 static inline void calculate_lanczos_coeffs(float t, float *coeffs)
475 for (int i = 0; i < 4; i++) {
476 const float x = M_PI * (t - i + 1);
480 coeffs[i] = sinf(x) * sinf(x / 2.f) / (x * x / 2.f);
485 for (int i = 0; i < 4; i++) {
491 * Calculate kernel for lanczos interpolation.
493 * @param du horizontal relative coordinate
494 * @param dv vertical relative coordinate
495 * @param rmap calculated 4x4 window
496 * @param u u remap data
497 * @param v v remap data
498 * @param ker ker remap data
500 static void lanczos_kernel(float du, float dv, const XYRemap *rmap,
501 int16_t *u, int16_t *v, int16_t *ker)
506 calculate_lanczos_coeffs(du, du_coeffs);
507 calculate_lanczos_coeffs(dv, dv_coeffs);
509 for (int i = 0; i < 4; i++) {
510 for (int j = 0; j < 4; j++) {
511 u[i * 4 + j] = rmap->u[i][j];
512 v[i * 4 + j] = rmap->v[i][j];
513 ker[i * 4 + j] = lrintf(du_coeffs[j] * dv_coeffs[i] * 16385.f);
519 * Calculate 1-dimensional spline16 coefficients.
521 * @param t relative coordinate
522 * @param coeffs coefficients
524 static void calculate_spline16_coeffs(float t, float *coeffs)
526 coeffs[0] = ((-1.f / 3.f * t + 0.8f) * t - 7.f / 15.f) * t;
527 coeffs[1] = ((t - 9.f / 5.f) * t - 0.2f) * t + 1.f;
528 coeffs[2] = ((6.f / 5.f - t) * t + 0.8f) * t;
529 coeffs[3] = ((1.f / 3.f * t - 0.2f) * t - 2.f / 15.f) * t;
533 * Calculate kernel for spline16 interpolation.
535 * @param du horizontal relative coordinate
536 * @param dv vertical relative coordinate
537 * @param rmap calculated 4x4 window
538 * @param u u remap data
539 * @param v v remap data
540 * @param ker ker remap data
542 static void spline16_kernel(float du, float dv, const XYRemap *rmap,
543 int16_t *u, int16_t *v, int16_t *ker)
548 calculate_spline16_coeffs(du, du_coeffs);
549 calculate_spline16_coeffs(dv, dv_coeffs);
551 for (int i = 0; i < 4; i++) {
552 for (int j = 0; j < 4; j++) {
553 u[i * 4 + j] = rmap->u[i][j];
554 v[i * 4 + j] = rmap->v[i][j];
555 ker[i * 4 + j] = lrintf(du_coeffs[j] * dv_coeffs[i] * 16385.f);
561 * Calculate 1-dimensional gaussian coefficients.
563 * @param t relative coordinate
564 * @param coeffs coefficients
566 static void calculate_gaussian_coeffs(float t, float *coeffs)
570 for (int i = 0; i < 4; i++) {
571 const float x = t - (i - 1);
575 coeffs[i] = expf(-2.f * x * x) * expf(-x * x / 2.f);
580 for (int i = 0; i < 4; i++) {
586 * Calculate kernel for gaussian interpolation.
588 * @param du horizontal relative coordinate
589 * @param dv vertical relative coordinate
590 * @param rmap calculated 4x4 window
591 * @param u u remap data
592 * @param v v remap data
593 * @param ker ker remap data
595 static void gaussian_kernel(float du, float dv, const XYRemap *rmap,
596 int16_t *u, int16_t *v, int16_t *ker)
601 calculate_gaussian_coeffs(du, du_coeffs);
602 calculate_gaussian_coeffs(dv, dv_coeffs);
604 for (int i = 0; i < 4; i++) {
605 for (int j = 0; j < 4; j++) {
606 u[i * 4 + j] = rmap->u[i][j];
607 v[i * 4 + j] = rmap->v[i][j];
608 ker[i * 4 + j] = lrintf(du_coeffs[j] * dv_coeffs[i] * 16385.f);
614 * Modulo operation with only positive remainders.
619 * @return positive remainder of (a / b)
621 static inline int mod(int a, int b)
623 const int res = a % b;
632 * Reflect y operation.
634 * @param y input vertical position
635 * @param h input height
637 static inline int reflecty(int y, int h)
642 return 2 * h - 1 - y;
649 * Reflect x operation for equirect.
651 * @param x input horizontal position
652 * @param y input vertical position
653 * @param w input width
654 * @param h input height
656 static inline int ereflectx(int x, int y, int w, int h)
665 * Reflect x operation.
667 * @param x input horizontal position
668 * @param y input vertical position
669 * @param w input width
670 * @param h input height
672 static inline int reflectx(int x, int y, int w, int h)
681 * Convert char to corresponding direction.
682 * Used for cubemap options.
684 static int get_direction(char c)
705 * Convert char to corresponding rotation angle.
706 * Used for cubemap options.
708 static int get_rotation(char c)
725 * Convert char to corresponding rotation order.
727 static int get_rorder(char c)
745 * Prepare data for processing cubemap input format.
747 * @param ctx filter context
751 static int prepare_cube_in(AVFilterContext *ctx)
753 V360Context *s = ctx->priv;
755 for (int face = 0; face < NB_FACES; face++) {
756 const char c = s->in_forder[face];
760 av_log(ctx, AV_LOG_ERROR,
761 "Incomplete in_forder option. Direction for all 6 faces should be specified.\n");
762 return AVERROR(EINVAL);
765 direction = get_direction(c);
766 if (direction == -1) {
767 av_log(ctx, AV_LOG_ERROR,
768 "Incorrect direction symbol '%c' in in_forder option.\n", c);
769 return AVERROR(EINVAL);
772 s->in_cubemap_face_order[direction] = face;
775 for (int face = 0; face < NB_FACES; face++) {
776 const char c = s->in_frot[face];
780 av_log(ctx, AV_LOG_ERROR,
781 "Incomplete in_frot option. Rotation for all 6 faces should be specified.\n");
782 return AVERROR(EINVAL);
785 rotation = get_rotation(c);
786 if (rotation == -1) {
787 av_log(ctx, AV_LOG_ERROR,
788 "Incorrect rotation symbol '%c' in in_frot option.\n", c);
789 return AVERROR(EINVAL);
792 s->in_cubemap_face_rotation[face] = rotation;
799 * Prepare data for processing cubemap output format.
801 * @param ctx filter context
805 static int prepare_cube_out(AVFilterContext *ctx)
807 V360Context *s = ctx->priv;
809 for (int face = 0; face < NB_FACES; face++) {
810 const char c = s->out_forder[face];
814 av_log(ctx, AV_LOG_ERROR,
815 "Incomplete out_forder option. Direction for all 6 faces should be specified.\n");
816 return AVERROR(EINVAL);
819 direction = get_direction(c);
820 if (direction == -1) {
821 av_log(ctx, AV_LOG_ERROR,
822 "Incorrect direction symbol '%c' in out_forder option.\n", c);
823 return AVERROR(EINVAL);
826 s->out_cubemap_direction_order[face] = direction;
829 for (int face = 0; face < NB_FACES; face++) {
830 const char c = s->out_frot[face];
834 av_log(ctx, AV_LOG_ERROR,
835 "Incomplete out_frot option. Rotation for all 6 faces should be specified.\n");
836 return AVERROR(EINVAL);
839 rotation = get_rotation(c);
840 if (rotation == -1) {
841 av_log(ctx, AV_LOG_ERROR,
842 "Incorrect rotation symbol '%c' in out_frot option.\n", c);
843 return AVERROR(EINVAL);
846 s->out_cubemap_face_rotation[face] = rotation;
852 static inline void rotate_cube_face(float *uf, float *vf, int rotation)
878 static inline void rotate_cube_face_inverse(float *uf, float *vf, int rotation)
909 static void normalize_vector(float *vec)
911 const float norm = sqrtf(vec[0] * vec[0] + vec[1] * vec[1] + vec[2] * vec[2]);
919 * Calculate 3D coordinates on sphere for corresponding cubemap position.
920 * Common operation for every cubemap.
922 * @param s filter private context
923 * @param uf horizontal cubemap coordinate [0, 1)
924 * @param vf vertical cubemap coordinate [0, 1)
925 * @param face face of cubemap
926 * @param vec coordinates on sphere
927 * @param scalew scale for uf
928 * @param scaleh scale for vf
930 static void cube_to_xyz(const V360Context *s,
931 float uf, float vf, int face,
932 float *vec, float scalew, float scaleh)
934 const int direction = s->out_cubemap_direction_order[face];
940 rotate_cube_face_inverse(&uf, &vf, s->out_cubemap_face_rotation[face]);
981 normalize_vector(vec);
985 * Calculate cubemap position for corresponding 3D coordinates on sphere.
986 * Common operation for every cubemap.
988 * @param s filter private context
989 * @param vec coordinated on sphere
990 * @param uf horizontal cubemap coordinate [0, 1)
991 * @param vf vertical cubemap coordinate [0, 1)
992 * @param direction direction of view
994 static void xyz_to_cube(const V360Context *s,
996 float *uf, float *vf, int *direction)
998 const float phi = atan2f(vec[0], -vec[2]);
999 const float theta = asinf(-vec[1]);
1000 float phi_norm, theta_threshold;
1003 if (phi >= -M_PI_4 && phi < M_PI_4) {
1006 } else if (phi >= -(M_PI_2 + M_PI_4) && phi < -M_PI_4) {
1008 phi_norm = phi + M_PI_2;
1009 } else if (phi >= M_PI_4 && phi < M_PI_2 + M_PI_4) {
1011 phi_norm = phi - M_PI_2;
1014 phi_norm = phi + ((phi > 0.f) ? -M_PI : M_PI);
1017 theta_threshold = atanf(cosf(phi_norm));
1018 if (theta > theta_threshold) {
1020 } else if (theta < -theta_threshold) {
1024 switch (*direction) {
1026 *uf = vec[2] / vec[0];
1027 *vf = -vec[1] / vec[0];
1030 *uf = vec[2] / vec[0];
1031 *vf = vec[1] / vec[0];
1034 *uf = vec[0] / vec[1];
1035 *vf = -vec[2] / vec[1];
1038 *uf = -vec[0] / vec[1];
1039 *vf = -vec[2] / vec[1];
1042 *uf = -vec[0] / vec[2];
1043 *vf = vec[1] / vec[2];
1046 *uf = -vec[0] / vec[2];
1047 *vf = -vec[1] / vec[2];
1053 face = s->in_cubemap_face_order[*direction];
1054 rotate_cube_face(uf, vf, s->in_cubemap_face_rotation[face]);
1056 (*uf) *= s->input_mirror_modifier[0];
1057 (*vf) *= s->input_mirror_modifier[1];
1061 * Find position on another cube face in case of overflow/underflow.
1062 * Used for calculation of interpolation window.
1064 * @param s filter private context
1065 * @param uf horizontal cubemap coordinate
1066 * @param vf vertical cubemap coordinate
1067 * @param direction direction of view
1068 * @param new_uf new horizontal cubemap coordinate
1069 * @param new_vf new vertical cubemap coordinate
1070 * @param face face position on cubemap
1072 static void process_cube_coordinates(const V360Context *s,
1073 float uf, float vf, int direction,
1074 float *new_uf, float *new_vf, int *face)
1077 * Cubemap orientation
1084 * +-------+-------+-------+-------+ ^ e |
1086 * | left | front | right | back | | g |
1087 * +-------+-------+-------+-------+ v h v
1093 *face = s->in_cubemap_face_order[direction];
1094 rotate_cube_face_inverse(&uf, &vf, s->in_cubemap_face_rotation[*face]);
1096 if ((uf < -1.f || uf >= 1.f) && (vf < -1.f || vf >= 1.f)) {
1097 // There are no pixels to use in this case
1100 } else if (uf < -1.f) {
1102 switch (direction) {
1136 } else if (uf >= 1.f) {
1138 switch (direction) {
1172 } else if (vf < -1.f) {
1174 switch (direction) {
1208 } else if (vf >= 1.f) {
1210 switch (direction) {
1250 *face = s->in_cubemap_face_order[direction];
1251 rotate_cube_face(new_uf, new_vf, s->in_cubemap_face_rotation[*face]);
1255 * Calculate 3D coordinates on sphere for corresponding frame position in cubemap3x2 format.
1257 * @param s filter private context
1258 * @param i horizontal position on frame [0, width)
1259 * @param j vertical position on frame [0, height)
1260 * @param width frame width
1261 * @param height frame height
1262 * @param vec coordinates on sphere
1264 static int cube3x2_to_xyz(const V360Context *s,
1265 int i, int j, int width, int height,
1268 const float scalew = s->fout_pad > 0 ? 1.f - s->fout_pad / (width / 3.f) : 1.f - s->out_pad;
1269 const float scaleh = s->fout_pad > 0 ? 1.f - s->fout_pad / (height / 2.f) : 1.f - s->out_pad;
1271 const float ew = width / 3.f;
1272 const float eh = height / 2.f;
1274 const int u_face = floorf(i / ew);
1275 const int v_face = floorf(j / eh);
1276 const int face = u_face + 3 * v_face;
1278 const int u_shift = ceilf(ew * u_face);
1279 const int v_shift = ceilf(eh * v_face);
1280 const int ewi = ceilf(ew * (u_face + 1)) - u_shift;
1281 const int ehi = ceilf(eh * (v_face + 1)) - v_shift;
1283 const float uf = 2.f * (i - u_shift + 0.5f) / ewi - 1.f;
1284 const float vf = 2.f * (j - v_shift + 0.5f) / ehi - 1.f;
1286 cube_to_xyz(s, uf, vf, face, vec, scalew, scaleh);
1292 * Calculate frame position in cubemap3x2 format for corresponding 3D coordinates on sphere.
1294 * @param s filter private context
1295 * @param vec coordinates on sphere
1296 * @param width frame width
1297 * @param height frame height
1298 * @param us horizontal coordinates for interpolation window
1299 * @param vs vertical coordinates for interpolation window
1300 * @param du horizontal relative coordinate
1301 * @param dv vertical relative coordinate
1303 static int xyz_to_cube3x2(const V360Context *s,
1304 const float *vec, int width, int height,
1305 int16_t us[4][4], int16_t vs[4][4], float *du, float *dv)
1307 const float scalew = s->fin_pad > 0 ? 1.f - s->fin_pad / (width / 3.f) : 1.f - s->in_pad;
1308 const float scaleh = s->fin_pad > 0 ? 1.f - s->fin_pad / (height / 2.f) : 1.f - s->in_pad;
1309 const float ew = width / 3.f;
1310 const float eh = height / 2.f;
1314 int direction, face;
1317 xyz_to_cube(s, vec, &uf, &vf, &direction);
1322 face = s->in_cubemap_face_order[direction];
1325 ewi = ceilf(ew * (u_face + 1)) - ceilf(ew * u_face);
1326 ehi = ceilf(eh * (v_face + 1)) - ceilf(eh * v_face);
1328 uf = 0.5f * ewi * (uf + 1.f) - 0.5f;
1329 vf = 0.5f * ehi * (vf + 1.f) - 0.5f;
1337 for (int i = 0; i < 4; i++) {
1338 for (int j = 0; j < 4; j++) {
1339 int new_ui = ui + j - 1;
1340 int new_vi = vi + i - 1;
1341 int u_shift, v_shift;
1342 int new_ewi, new_ehi;
1344 if (new_ui >= 0 && new_ui < ewi && new_vi >= 0 && new_vi < ehi) {
1345 face = s->in_cubemap_face_order[direction];
1349 u_shift = ceilf(ew * u_face);
1350 v_shift = ceilf(eh * v_face);
1352 uf = 2.f * new_ui / ewi - 1.f;
1353 vf = 2.f * new_vi / ehi - 1.f;
1358 process_cube_coordinates(s, uf, vf, direction, &uf, &vf, &face);
1365 u_shift = ceilf(ew * u_face);
1366 v_shift = ceilf(eh * v_face);
1367 new_ewi = ceilf(ew * (u_face + 1)) - u_shift;
1368 new_ehi = ceilf(eh * (v_face + 1)) - v_shift;
1370 new_ui = av_clip(lrintf(0.5f * new_ewi * (uf + 1.f)), 0, new_ewi - 1);
1371 new_vi = av_clip(lrintf(0.5f * new_ehi * (vf + 1.f)), 0, new_ehi - 1);
1374 us[i][j] = u_shift + new_ui;
1375 vs[i][j] = v_shift + new_vi;
1383 * Calculate 3D coordinates on sphere for corresponding frame position in cubemap1x6 format.
1385 * @param s filter private context
1386 * @param i horizontal position on frame [0, width)
1387 * @param j vertical position on frame [0, height)
1388 * @param width frame width
1389 * @param height frame height
1390 * @param vec coordinates on sphere
1392 static int cube1x6_to_xyz(const V360Context *s,
1393 int i, int j, int width, int height,
1396 const float scalew = s->fout_pad > 0 ? 1.f - (float)(s->fout_pad) / width : 1.f - s->out_pad;
1397 const float scaleh = s->fout_pad > 0 ? 1.f - s->fout_pad / (height / 6.f) : 1.f - s->out_pad;
1399 const float ew = width;
1400 const float eh = height / 6.f;
1402 const int face = floorf(j / eh);
1404 const int v_shift = ceilf(eh * face);
1405 const int ehi = ceilf(eh * (face + 1)) - v_shift;
1407 const float uf = 2.f * (i + 0.5f) / ew - 1.f;
1408 const float vf = 2.f * (j - v_shift + 0.5f) / ehi - 1.f;
1410 cube_to_xyz(s, uf, vf, face, vec, scalew, scaleh);
1416 * Calculate 3D coordinates on sphere for corresponding frame position in cubemap6x1 format.
1418 * @param s filter private context
1419 * @param i horizontal position on frame [0, width)
1420 * @param j vertical position on frame [0, height)
1421 * @param width frame width
1422 * @param height frame height
1423 * @param vec coordinates on sphere
1425 static int cube6x1_to_xyz(const V360Context *s,
1426 int i, int j, int width, int height,
1429 const float scalew = s->fout_pad > 0 ? 1.f - s->fout_pad / (width / 6.f) : 1.f - s->out_pad;
1430 const float scaleh = s->fout_pad > 0 ? 1.f - (float)(s->fout_pad) / height : 1.f - s->out_pad;
1432 const float ew = width / 6.f;
1433 const float eh = height;
1435 const int face = floorf(i / ew);
1437 const int u_shift = ceilf(ew * face);
1438 const int ewi = ceilf(ew * (face + 1)) - u_shift;
1440 const float uf = 2.f * (i - u_shift + 0.5f) / ewi - 1.f;
1441 const float vf = 2.f * (j + 0.5f) / eh - 1.f;
1443 cube_to_xyz(s, uf, vf, face, vec, scalew, scaleh);
1449 * Calculate frame position in cubemap1x6 format for corresponding 3D coordinates on sphere.
1451 * @param s filter private context
1452 * @param vec coordinates on sphere
1453 * @param width frame width
1454 * @param height frame height
1455 * @param us horizontal coordinates for interpolation window
1456 * @param vs vertical coordinates for interpolation window
1457 * @param du horizontal relative coordinate
1458 * @param dv vertical relative coordinate
1460 static int xyz_to_cube1x6(const V360Context *s,
1461 const float *vec, int width, int height,
1462 int16_t us[4][4], int16_t vs[4][4], float *du, float *dv)
1464 const float scalew = s->fin_pad > 0 ? 1.f - (float)(s->fin_pad) / width : 1.f - s->in_pad;
1465 const float scaleh = s->fin_pad > 0 ? 1.f - s->fin_pad / (height / 6.f) : 1.f - s->in_pad;
1466 const float eh = height / 6.f;
1467 const int ewi = width;
1471 int direction, face;
1473 xyz_to_cube(s, vec, &uf, &vf, &direction);
1478 face = s->in_cubemap_face_order[direction];
1479 ehi = ceilf(eh * (face + 1)) - ceilf(eh * face);
1481 uf = 0.5f * ewi * (uf + 1.f) - 0.5f;
1482 vf = 0.5f * ehi * (vf + 1.f) - 0.5f;
1490 for (int i = 0; i < 4; i++) {
1491 for (int j = 0; j < 4; j++) {
1492 int new_ui = ui + j - 1;
1493 int new_vi = vi + i - 1;
1497 if (new_ui >= 0 && new_ui < ewi && new_vi >= 0 && new_vi < ehi) {
1498 face = s->in_cubemap_face_order[direction];
1500 v_shift = ceilf(eh * face);
1502 uf = 2.f * new_ui / ewi - 1.f;
1503 vf = 2.f * new_vi / ehi - 1.f;
1508 process_cube_coordinates(s, uf, vf, direction, &uf, &vf, &face);
1513 v_shift = ceilf(eh * face);
1514 new_ehi = ceilf(eh * (face + 1)) - v_shift;
1516 new_ui = av_clip(lrintf(0.5f * ewi * (uf + 1.f)), 0, ewi - 1);
1517 new_vi = av_clip(lrintf(0.5f * new_ehi * (vf + 1.f)), 0, new_ehi - 1);
1521 vs[i][j] = v_shift + new_vi;
1529 * Calculate frame position in cubemap6x1 format for corresponding 3D coordinates on sphere.
1531 * @param s filter private context
1532 * @param vec coordinates on sphere
1533 * @param width frame width
1534 * @param height frame height
1535 * @param us horizontal coordinates for interpolation window
1536 * @param vs vertical coordinates for interpolation window
1537 * @param du horizontal relative coordinate
1538 * @param dv vertical relative coordinate
1540 static int xyz_to_cube6x1(const V360Context *s,
1541 const float *vec, int width, int height,
1542 int16_t us[4][4], int16_t vs[4][4], float *du, float *dv)
1544 const float scalew = s->fin_pad > 0 ? 1.f - s->fin_pad / (width / 6.f) : 1.f - s->in_pad;
1545 const float scaleh = s->fin_pad > 0 ? 1.f - (float)(s->fin_pad) / height : 1.f - s->in_pad;
1546 const float ew = width / 6.f;
1547 const int ehi = height;
1551 int direction, face;
1553 xyz_to_cube(s, vec, &uf, &vf, &direction);
1558 face = s->in_cubemap_face_order[direction];
1559 ewi = ceilf(ew * (face + 1)) - ceilf(ew * face);
1561 uf = 0.5f * ewi * (uf + 1.f) - 0.5f;
1562 vf = 0.5f * ehi * (vf + 1.f) - 0.5f;
1570 for (int i = 0; i < 4; i++) {
1571 for (int j = 0; j < 4; j++) {
1572 int new_ui = ui + j - 1;
1573 int new_vi = vi + i - 1;
1577 if (new_ui >= 0 && new_ui < ewi && new_vi >= 0 && new_vi < ehi) {
1578 face = s->in_cubemap_face_order[direction];
1580 u_shift = ceilf(ew * face);
1582 uf = 2.f * new_ui / ewi - 1.f;
1583 vf = 2.f * new_vi / ehi - 1.f;
1588 process_cube_coordinates(s, uf, vf, direction, &uf, &vf, &face);
1593 u_shift = ceilf(ew * face);
1594 new_ewi = ceilf(ew * (face + 1)) - u_shift;
1596 new_ui = av_clip(lrintf(0.5f * new_ewi * (uf + 1.f)), 0, new_ewi - 1);
1597 new_vi = av_clip(lrintf(0.5f * ehi * (vf + 1.f)), 0, ehi - 1);
1600 us[i][j] = u_shift + new_ui;
1609 * Calculate 3D coordinates on sphere for corresponding frame position in equirectangular format.
1611 * @param s filter private context
1612 * @param i horizontal position on frame [0, width)
1613 * @param j vertical position on frame [0, height)
1614 * @param width frame width
1615 * @param height frame height
1616 * @param vec coordinates on sphere
1618 static int equirect_to_xyz(const V360Context *s,
1619 int i, int j, int width, int height,
1622 const float phi = ((2.f * i + 0.5f) / width - 1.f) * M_PI;
1623 const float theta = ((2.f * j + 0.5f) / height - 1.f) * M_PI_2;
1625 const float sin_phi = sinf(phi);
1626 const float cos_phi = cosf(phi);
1627 const float sin_theta = sinf(theta);
1628 const float cos_theta = cosf(theta);
1630 vec[0] = cos_theta * sin_phi;
1631 vec[1] = -sin_theta;
1632 vec[2] = -cos_theta * cos_phi;
1638 * Calculate 3D coordinates on sphere for corresponding frame position in half equirectangular format.
1640 * @param s filter private context
1641 * @param i horizontal position on frame [0, width)
1642 * @param j vertical position on frame [0, height)
1643 * @param width frame width
1644 * @param height frame height
1645 * @param vec coordinates on sphere
1647 static int hequirect_to_xyz(const V360Context *s,
1648 int i, int j, int width, int height,
1651 const float phi = ((2.f * i + 0.5f) / width - 1.f) * M_PI_2;
1652 const float theta = ((2.f * j + 0.5f) / height - 1.f) * M_PI_2;
1654 const float sin_phi = sinf(phi);
1655 const float cos_phi = cosf(phi);
1656 const float sin_theta = sinf(theta);
1657 const float cos_theta = cosf(theta);
1659 vec[0] = cos_theta * sin_phi;
1660 vec[1] = -sin_theta;
1661 vec[2] = -cos_theta * cos_phi;
1667 * Prepare data for processing stereographic output format.
1669 * @param ctx filter context
1671 * @return error code
1673 static int prepare_stereographic_out(AVFilterContext *ctx)
1675 V360Context *s = ctx->priv;
1677 s->flat_range[0] = tanf(FFMIN(s->h_fov, 359.f) * M_PI / 720.f);
1678 s->flat_range[1] = tanf(FFMIN(s->v_fov, 359.f) * M_PI / 720.f);
1684 * Calculate 3D coordinates on sphere for corresponding frame position in stereographic format.
1686 * @param s filter private context
1687 * @param i horizontal position on frame [0, width)
1688 * @param j vertical position on frame [0, height)
1689 * @param width frame width
1690 * @param height frame height
1691 * @param vec coordinates on sphere
1693 static int stereographic_to_xyz(const V360Context *s,
1694 int i, int j, int width, int height,
1697 const float x = ((2.f * i + 1.f) / width - 1.f) * s->flat_range[0];
1698 const float y = ((2.f * j + 1.f) / height - 1.f) * s->flat_range[1];
1699 const float r = hypotf(x, y);
1700 const float theta = atanf(r) * 2.f;
1701 const float sin_theta = sinf(theta);
1703 vec[0] = x / r * sin_theta;
1704 vec[1] = -y / r * sin_theta;
1705 vec[2] = -cosf(theta);
1707 normalize_vector(vec);
1713 * Prepare data for processing stereographic input format.
1715 * @param ctx filter context
1717 * @return error code
1719 static int prepare_stereographic_in(AVFilterContext *ctx)
1721 V360Context *s = ctx->priv;
1723 s->iflat_range[0] = tanf(FFMIN(s->ih_fov, 359.f) * M_PI / 720.f);
1724 s->iflat_range[1] = tanf(FFMIN(s->iv_fov, 359.f) * M_PI / 720.f);
1730 * Calculate frame position in stereographic format for corresponding 3D coordinates on sphere.
1732 * @param s filter private context
1733 * @param vec coordinates on sphere
1734 * @param width frame width
1735 * @param height frame height
1736 * @param us horizontal coordinates for interpolation window
1737 * @param vs vertical coordinates for interpolation window
1738 * @param du horizontal relative coordinate
1739 * @param dv vertical relative coordinate
1741 static int xyz_to_stereographic(const V360Context *s,
1742 const float *vec, int width, int height,
1743 int16_t us[4][4], int16_t vs[4][4], float *du, float *dv)
1745 const float theta = acosf(-vec[2]);
1746 const float r = tanf(theta * 0.5f);
1747 const float c = r / hypotf(vec[0], vec[1]);
1748 const float x = vec[0] * c / s->iflat_range[0] * s->input_mirror_modifier[0];
1749 const float y = -vec[1] * c / s->iflat_range[1] * s->input_mirror_modifier[1];
1751 const float uf = (x + 1.f) * width / 2.f;
1752 const float vf = (y + 1.f) * height / 2.f;
1754 const int ui = floorf(uf);
1755 const int vi = floorf(vf);
1757 const int visible = isfinite(x) && isfinite(y) && vi >= 0 && vi < height && ui >= 0 && ui < width;
1759 *du = visible ? uf - ui : 0.f;
1760 *dv = visible ? vf - vi : 0.f;
1762 for (int i = 0; i < 4; i++) {
1763 for (int j = 0; j < 4; j++) {
1764 us[i][j] = visible ? av_clip(ui + j - 1, 0, width - 1) : 0;
1765 vs[i][j] = visible ? av_clip(vi + i - 1, 0, height - 1) : 0;
1773 * Calculate frame position in equirectangular format for corresponding 3D coordinates on sphere.
1775 * @param s filter private context
1776 * @param vec coordinates on sphere
1777 * @param width frame width
1778 * @param height frame height
1779 * @param us horizontal coordinates for interpolation window
1780 * @param vs vertical coordinates for interpolation window
1781 * @param du horizontal relative coordinate
1782 * @param dv vertical relative coordinate
1784 static int xyz_to_equirect(const V360Context *s,
1785 const float *vec, int width, int height,
1786 int16_t us[4][4], int16_t vs[4][4], float *du, float *dv)
1788 const float phi = atan2f(vec[0], -vec[2]) * s->input_mirror_modifier[0];
1789 const float theta = asinf(-vec[1]) * s->input_mirror_modifier[1];
1791 const float uf = (phi / M_PI + 1.f) * width / 2.f;
1792 const float vf = (theta / M_PI_2 + 1.f) * height / 2.f;
1794 const int ui = floorf(uf);
1795 const int vi = floorf(vf);
1800 for (int i = 0; i < 4; i++) {
1801 for (int j = 0; j < 4; j++) {
1802 us[i][j] = ereflectx(ui + j - 1, vi + i - 1, width, height);
1803 vs[i][j] = reflecty(vi + i - 1, height);
1811 * Calculate frame position in half equirectangular format for corresponding 3D coordinates on sphere.
1813 * @param s filter private context
1814 * @param vec coordinates on sphere
1815 * @param width frame width
1816 * @param height frame height
1817 * @param us horizontal coordinates for interpolation window
1818 * @param vs vertical coordinates for interpolation window
1819 * @param du horizontal relative coordinate
1820 * @param dv vertical relative coordinate
1822 static int xyz_to_hequirect(const V360Context *s,
1823 const float *vec, int width, int height,
1824 int16_t us[4][4], int16_t vs[4][4], float *du, float *dv)
1826 const float phi = atan2f(vec[0], -vec[2]) * s->input_mirror_modifier[0];
1827 const float theta = asinf(-vec[1]) * s->input_mirror_modifier[1];
1829 const float uf = (phi / M_PI_2 + 1.f) * width / 2.f;
1830 const float vf = (theta / M_PI_2 + 1.f) * height / 2.f;
1832 const int ui = floorf(uf);
1833 const int vi = floorf(vf);
1835 const int visible = phi >= -M_PI_2 && phi <= M_PI_2;
1840 for (int i = 0; i < 4; i++) {
1841 for (int j = 0; j < 4; j++) {
1842 us[i][j] = av_clip(ui + j - 1, 0, width - 1);
1843 vs[i][j] = av_clip(vi + i - 1, 0, height - 1);
1851 * Prepare data for processing flat input format.
1853 * @param ctx filter context
1855 * @return error code
1857 static int prepare_flat_in(AVFilterContext *ctx)
1859 V360Context *s = ctx->priv;
1861 s->iflat_range[0] = tanf(0.5f * s->ih_fov * M_PI / 180.f);
1862 s->iflat_range[1] = tanf(0.5f * s->iv_fov * M_PI / 180.f);
1868 * Calculate frame position in flat format for corresponding 3D coordinates on sphere.
1870 * @param s filter private context
1871 * @param vec coordinates on sphere
1872 * @param width frame width
1873 * @param height frame height
1874 * @param us horizontal coordinates for interpolation window
1875 * @param vs vertical coordinates for interpolation window
1876 * @param du horizontal relative coordinate
1877 * @param dv vertical relative coordinate
1879 static int xyz_to_flat(const V360Context *s,
1880 const float *vec, int width, int height,
1881 int16_t us[4][4], int16_t vs[4][4], float *du, float *dv)
1883 const float theta = acosf(vec[2]);
1884 const float r = tanf(theta);
1885 const float rr = fabsf(r) < 1e+6f ? r : hypotf(width, height);
1886 const float zf = -vec[2];
1887 const float h = hypotf(vec[0], vec[1]);
1888 const float c = h <= 1e-6f ? 1.f : rr / h;
1889 float uf = -vec[0] * c / s->iflat_range[0] * s->input_mirror_modifier[0];
1890 float vf = vec[1] * c / s->iflat_range[1] * s->input_mirror_modifier[1];
1891 int visible, ui, vi;
1893 uf = zf >= 0.f ? (uf + 1.f) * width / 2.f : 0.f;
1894 vf = zf >= 0.f ? (vf + 1.f) * height / 2.f : 0.f;
1899 visible = vi >= 0 && vi < height && ui >= 0 && ui < width && zf >= 0.f;
1904 for (int i = 0; i < 4; i++) {
1905 for (int j = 0; j < 4; j++) {
1906 us[i][j] = visible ? av_clip(ui + j - 1, 0, width - 1) : 0;
1907 vs[i][j] = visible ? av_clip(vi + i - 1, 0, height - 1) : 0;
1915 * Calculate frame position in mercator format for corresponding 3D coordinates on sphere.
1917 * @param s filter private context
1918 * @param vec coordinates on sphere
1919 * @param width frame width
1920 * @param height frame height
1921 * @param us horizontal coordinates for interpolation window
1922 * @param vs vertical coordinates for interpolation window
1923 * @param du horizontal relative coordinate
1924 * @param dv vertical relative coordinate
1926 static int xyz_to_mercator(const V360Context *s,
1927 const float *vec, int width, int height,
1928 int16_t us[4][4], int16_t vs[4][4], float *du, float *dv)
1930 const float phi = atan2f(vec[0], -vec[2]) * s->input_mirror_modifier[0];
1931 const float theta = -vec[1] * s->input_mirror_modifier[1];
1933 const float uf = (phi / M_PI + 1.f) * width / 2.f;
1934 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;
1936 const int ui = floorf(uf);
1937 const int vi = floorf(vf);
1942 for (int i = 0; i < 4; i++) {
1943 for (int j = 0; j < 4; j++) {
1944 us[i][j] = av_clip(ui + j - 1, 0, width - 1);
1945 vs[i][j] = av_clip(vi + i - 1, 0, height - 1);
1953 * Calculate 3D coordinates on sphere for corresponding frame position in mercator format.
1955 * @param s filter private context
1956 * @param i horizontal position on frame [0, width)
1957 * @param j vertical position on frame [0, height)
1958 * @param width frame width
1959 * @param height frame height
1960 * @param vec coordinates on sphere
1962 static int mercator_to_xyz(const V360Context *s,
1963 int i, int j, int width, int height,
1966 const float phi = ((2.f * i + 1.f) / width - 1.f) * M_PI + M_PI_2;
1967 const float y = ((2.f * j + 1.f) / height - 1.f) * M_PI;
1968 const float div = expf(2.f * y) + 1.f;
1970 const float sin_phi = sinf(phi);
1971 const float cos_phi = cosf(phi);
1972 const float sin_theta = -2.f * expf(y) / div;
1973 const float cos_theta = -(expf(2.f * y) - 1.f) / div;
1975 vec[0] = sin_theta * cos_phi;
1977 vec[2] = sin_theta * sin_phi;
1983 * Calculate frame position in ball format for corresponding 3D coordinates on sphere.
1985 * @param s filter private context
1986 * @param vec coordinates on sphere
1987 * @param width frame width
1988 * @param height frame height
1989 * @param us horizontal coordinates for interpolation window
1990 * @param vs vertical coordinates for interpolation window
1991 * @param du horizontal relative coordinate
1992 * @param dv vertical relative coordinate
1994 static int xyz_to_ball(const V360Context *s,
1995 const float *vec, int width, int height,
1996 int16_t us[4][4], int16_t vs[4][4], float *du, float *dv)
1998 const float l = hypotf(vec[0], vec[1]);
1999 const float r = sqrtf(1.f + vec[2]) / M_SQRT2;
2001 const float uf = (1.f + r * vec[0] * s->input_mirror_modifier[0] / (l > 0.f ? l : 1.f)) * width * 0.5f;
2002 const float vf = (1.f - r * vec[1] * s->input_mirror_modifier[1] / (l > 0.f ? l : 1.f)) * height * 0.5f;
2004 const int ui = floorf(uf);
2005 const int vi = floorf(vf);
2010 for (int i = 0; i < 4; i++) {
2011 for (int j = 0; j < 4; j++) {
2012 us[i][j] = av_clip(ui + j - 1, 0, width - 1);
2013 vs[i][j] = av_clip(vi + i - 1, 0, height - 1);
2021 * Calculate 3D coordinates on sphere for corresponding frame position in ball format.
2023 * @param s filter private context
2024 * @param i horizontal position on frame [0, width)
2025 * @param j vertical position on frame [0, height)
2026 * @param width frame width
2027 * @param height frame height
2028 * @param vec coordinates on sphere
2030 static int ball_to_xyz(const V360Context *s,
2031 int i, int j, int width, int height,
2034 const float x = (2.f * i + 1.f) / width - 1.f;
2035 const float y = (2.f * j + 1.f) / height - 1.f;
2036 const float l = hypotf(x, y);
2039 const float z = 2.f * l * sqrtf(1.f - l * l);
2041 vec[0] = z * x / (l > 0.f ? l : 1.f);
2042 vec[1] = -z * y / (l > 0.f ? l : 1.f);
2043 vec[2] = -1.f + 2.f * l * l;
2055 * Calculate 3D coordinates on sphere for corresponding frame position in hammer 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 hammer_to_xyz(const V360Context *s,
2065 int i, int j, int width, int height,
2068 const float x = ((2.f * i + 1.f) / width - 1.f);
2069 const float y = ((2.f * j + 1.f) / height - 1.f);
2071 const float xx = x * x;
2072 const float yy = y * y;
2074 const float z = sqrtf(1.f - xx * 0.5f - yy * 0.5f);
2076 const float a = M_SQRT2 * x * z;
2077 const float b = 2.f * z * z - 1.f;
2079 const float aa = a * a;
2080 const float bb = b * b;
2082 const float w = sqrtf(1.f - 2.f * yy * z * z);
2084 vec[0] = w * 2.f * a * b / (aa + bb);
2085 vec[1] = -M_SQRT2 * y * z;
2086 vec[2] = -w * (bb - aa) / (aa + bb);
2088 normalize_vector(vec);
2094 * Calculate frame position in hammer format for corresponding 3D coordinates on sphere.
2096 * @param s filter private context
2097 * @param vec coordinates on sphere
2098 * @param width frame width
2099 * @param height frame height
2100 * @param us horizontal coordinates for interpolation window
2101 * @param vs vertical coordinates for interpolation window
2102 * @param du horizontal relative coordinate
2103 * @param dv vertical relative coordinate
2105 static int xyz_to_hammer(const V360Context *s,
2106 const float *vec, int width, int height,
2107 int16_t us[4][4], int16_t vs[4][4], float *du, float *dv)
2109 const float theta = atan2f(vec[0], -vec[2]) * s->input_mirror_modifier[0];
2111 const float z = sqrtf(1.f + sqrtf(1.f - vec[1] * vec[1]) * cosf(theta * 0.5f));
2112 const float x = sqrtf(1.f - vec[1] * vec[1]) * sinf(theta * 0.5f) / z;
2113 const float y = -vec[1] / z * s->input_mirror_modifier[1];
2115 const float uf = (x + 1.f) * width / 2.f;
2116 const float vf = (y + 1.f) * height / 2.f;
2118 const int ui = floorf(uf);
2119 const int vi = floorf(vf);
2124 for (int i = 0; i < 4; i++) {
2125 for (int j = 0; j < 4; j++) {
2126 us[i][j] = av_clip(ui + j - 1, 0, width - 1);
2127 vs[i][j] = av_clip(vi + i - 1, 0, height - 1);
2135 * Calculate 3D coordinates on sphere for corresponding frame position in sinusoidal format.
2137 * @param s filter private context
2138 * @param i horizontal position on frame [0, width)
2139 * @param j vertical position on frame [0, height)
2140 * @param width frame width
2141 * @param height frame height
2142 * @param vec coordinates on sphere
2144 static int sinusoidal_to_xyz(const V360Context *s,
2145 int i, int j, int width, int height,
2148 const float theta = ((2.f * j + 1.f) / height - 1.f) * M_PI_2;
2149 const float phi = ((2.f * i + 1.f) / width - 1.f) * M_PI / cosf(theta);
2151 const float sin_phi = sinf(phi);
2152 const float cos_phi = cosf(phi);
2153 const float sin_theta = sinf(theta);
2154 const float cos_theta = cosf(theta);
2156 vec[0] = cos_theta * sin_phi;
2157 vec[1] = -sin_theta;
2158 vec[2] = -cos_theta * cos_phi;
2160 normalize_vector(vec);
2166 * Calculate frame position in sinusoidal format for corresponding 3D coordinates on sphere.
2168 * @param s filter private context
2169 * @param vec coordinates on sphere
2170 * @param width frame width
2171 * @param height frame height
2172 * @param us horizontal coordinates for interpolation window
2173 * @param vs vertical coordinates for interpolation window
2174 * @param du horizontal relative coordinate
2175 * @param dv vertical relative coordinate
2177 static int xyz_to_sinusoidal(const V360Context *s,
2178 const float *vec, int width, int height,
2179 int16_t us[4][4], int16_t vs[4][4], float *du, float *dv)
2181 const float theta = asinf(-vec[1]) * s->input_mirror_modifier[1];
2182 const float phi = atan2f(vec[0], -vec[2]) * s->input_mirror_modifier[0] * cosf(theta);
2184 const float uf = (phi / M_PI + 1.f) * width / 2.f;
2185 const float vf = (theta / M_PI_2 + 1.f) * height / 2.f;
2187 const int ui = floorf(uf);
2188 const int vi = floorf(vf);
2193 for (int i = 0; i < 4; i++) {
2194 for (int j = 0; j < 4; j++) {
2195 us[i][j] = av_clip(ui + j - 1, 0, width - 1);
2196 vs[i][j] = av_clip(vi + i - 1, 0, height - 1);
2204 * Prepare data for processing equi-angular cubemap input format.
2206 * @param ctx filter context
2208 * @return error code
2210 static int prepare_eac_in(AVFilterContext *ctx)
2212 V360Context *s = ctx->priv;
2214 if (s->ih_flip && s->iv_flip) {
2215 s->in_cubemap_face_order[RIGHT] = BOTTOM_LEFT;
2216 s->in_cubemap_face_order[LEFT] = BOTTOM_RIGHT;
2217 s->in_cubemap_face_order[UP] = TOP_LEFT;
2218 s->in_cubemap_face_order[DOWN] = TOP_RIGHT;
2219 s->in_cubemap_face_order[FRONT] = BOTTOM_MIDDLE;
2220 s->in_cubemap_face_order[BACK] = TOP_MIDDLE;
2221 } else if (s->ih_flip) {
2222 s->in_cubemap_face_order[RIGHT] = TOP_LEFT;
2223 s->in_cubemap_face_order[LEFT] = TOP_RIGHT;
2224 s->in_cubemap_face_order[UP] = BOTTOM_LEFT;
2225 s->in_cubemap_face_order[DOWN] = BOTTOM_RIGHT;
2226 s->in_cubemap_face_order[FRONT] = TOP_MIDDLE;
2227 s->in_cubemap_face_order[BACK] = BOTTOM_MIDDLE;
2228 } else if (s->iv_flip) {
2229 s->in_cubemap_face_order[RIGHT] = BOTTOM_RIGHT;
2230 s->in_cubemap_face_order[LEFT] = BOTTOM_LEFT;
2231 s->in_cubemap_face_order[UP] = TOP_RIGHT;
2232 s->in_cubemap_face_order[DOWN] = TOP_LEFT;
2233 s->in_cubemap_face_order[FRONT] = BOTTOM_MIDDLE;
2234 s->in_cubemap_face_order[BACK] = TOP_MIDDLE;
2236 s->in_cubemap_face_order[RIGHT] = TOP_RIGHT;
2237 s->in_cubemap_face_order[LEFT] = TOP_LEFT;
2238 s->in_cubemap_face_order[UP] = BOTTOM_RIGHT;
2239 s->in_cubemap_face_order[DOWN] = BOTTOM_LEFT;
2240 s->in_cubemap_face_order[FRONT] = TOP_MIDDLE;
2241 s->in_cubemap_face_order[BACK] = BOTTOM_MIDDLE;
2245 s->in_cubemap_face_rotation[TOP_LEFT] = ROT_270;
2246 s->in_cubemap_face_rotation[TOP_MIDDLE] = ROT_90;
2247 s->in_cubemap_face_rotation[TOP_RIGHT] = ROT_270;
2248 s->in_cubemap_face_rotation[BOTTOM_LEFT] = ROT_0;
2249 s->in_cubemap_face_rotation[BOTTOM_MIDDLE] = ROT_0;
2250 s->in_cubemap_face_rotation[BOTTOM_RIGHT] = ROT_0;
2252 s->in_cubemap_face_rotation[TOP_LEFT] = ROT_0;
2253 s->in_cubemap_face_rotation[TOP_MIDDLE] = ROT_0;
2254 s->in_cubemap_face_rotation[TOP_RIGHT] = ROT_0;
2255 s->in_cubemap_face_rotation[BOTTOM_LEFT] = ROT_270;
2256 s->in_cubemap_face_rotation[BOTTOM_MIDDLE] = ROT_90;
2257 s->in_cubemap_face_rotation[BOTTOM_RIGHT] = ROT_270;
2264 * Prepare data for processing equi-angular cubemap output format.
2266 * @param ctx filter context
2268 * @return error code
2270 static int prepare_eac_out(AVFilterContext *ctx)
2272 V360Context *s = ctx->priv;
2274 s->out_cubemap_direction_order[TOP_LEFT] = LEFT;
2275 s->out_cubemap_direction_order[TOP_MIDDLE] = FRONT;
2276 s->out_cubemap_direction_order[TOP_RIGHT] = RIGHT;
2277 s->out_cubemap_direction_order[BOTTOM_LEFT] = DOWN;
2278 s->out_cubemap_direction_order[BOTTOM_MIDDLE] = BACK;
2279 s->out_cubemap_direction_order[BOTTOM_RIGHT] = UP;
2281 s->out_cubemap_face_rotation[TOP_LEFT] = ROT_0;
2282 s->out_cubemap_face_rotation[TOP_MIDDLE] = ROT_0;
2283 s->out_cubemap_face_rotation[TOP_RIGHT] = ROT_0;
2284 s->out_cubemap_face_rotation[BOTTOM_LEFT] = ROT_270;
2285 s->out_cubemap_face_rotation[BOTTOM_MIDDLE] = ROT_90;
2286 s->out_cubemap_face_rotation[BOTTOM_RIGHT] = ROT_270;
2292 * Calculate 3D coordinates on sphere for corresponding frame position in equi-angular cubemap format.
2294 * @param s filter private context
2295 * @param i horizontal position on frame [0, width)
2296 * @param j vertical position on frame [0, height)
2297 * @param width frame width
2298 * @param height frame height
2299 * @param vec coordinates on sphere
2301 static int eac_to_xyz(const V360Context *s,
2302 int i, int j, int width, int height,
2305 const float pixel_pad = 2;
2306 const float u_pad = pixel_pad / width;
2307 const float v_pad = pixel_pad / height;
2309 int u_face, v_face, face;
2311 float l_x, l_y, l_z;
2313 float uf = (i + 0.5f) / width;
2314 float vf = (j + 0.5f) / height;
2316 // EAC has 2-pixel padding on faces except between faces on the same row
2317 // Padding pixels seems not to be stretched with tangent as regular pixels
2318 // Formulas below approximate original padding as close as I could get experimentally
2320 // Horizontal padding
2321 uf = 3.f * (uf - u_pad) / (1.f - 2.f * u_pad);
2325 } else if (uf >= 3.f) {
2329 u_face = floorf(uf);
2330 uf = fmodf(uf, 1.f) - 0.5f;
2334 v_face = floorf(vf * 2.f);
2335 vf = (vf - v_pad - 0.5f * v_face) / (0.5f - 2.f * v_pad) - 0.5f;
2337 if (uf >= -0.5f && uf < 0.5f) {
2338 uf = tanf(M_PI_2 * uf);
2342 if (vf >= -0.5f && vf < 0.5f) {
2343 vf = tanf(M_PI_2 * vf);
2348 face = u_face + 3 * v_face;
2389 normalize_vector(vec);
2395 * Calculate frame position in equi-angular cubemap format for corresponding 3D coordinates on sphere.
2397 * @param s filter private context
2398 * @param vec coordinates on sphere
2399 * @param width frame width
2400 * @param height frame height
2401 * @param us horizontal coordinates for interpolation window
2402 * @param vs vertical coordinates for interpolation window
2403 * @param du horizontal relative coordinate
2404 * @param dv vertical relative coordinate
2406 static int xyz_to_eac(const V360Context *s,
2407 const float *vec, int width, int height,
2408 int16_t us[4][4], int16_t vs[4][4], float *du, float *dv)
2410 const float pixel_pad = 2;
2411 const float u_pad = pixel_pad / width;
2412 const float v_pad = pixel_pad / height;
2416 int direction, face;
2419 xyz_to_cube(s, vec, &uf, &vf, &direction);
2421 face = s->in_cubemap_face_order[direction];
2425 uf = M_2_PI * atanf(uf) + 0.5f;
2426 vf = M_2_PI * atanf(vf) + 0.5f;
2428 // These formulas are inversed from eac_to_xyz ones
2429 uf = (uf + u_face) * (1.f - 2.f * u_pad) / 3.f + u_pad;
2430 vf = vf * (0.5f - 2.f * v_pad) + v_pad + 0.5f * v_face;
2444 for (int i = 0; i < 4; i++) {
2445 for (int j = 0; j < 4; j++) {
2446 us[i][j] = av_clip(ui + j - 1, 0, width - 1);
2447 vs[i][j] = av_clip(vi + i - 1, 0, height - 1);
2455 * Prepare data for processing flat output format.
2457 * @param ctx filter context
2459 * @return error code
2461 static int prepare_flat_out(AVFilterContext *ctx)
2463 V360Context *s = ctx->priv;
2465 s->flat_range[0] = tanf(0.5f * s->h_fov * M_PI / 180.f);
2466 s->flat_range[1] = tanf(0.5f * s->v_fov * M_PI / 180.f);
2472 * Calculate 3D coordinates on sphere for corresponding frame position in flat format.
2474 * @param s filter private context
2475 * @param i horizontal position on frame [0, width)
2476 * @param j vertical position on frame [0, height)
2477 * @param width frame width
2478 * @param height frame height
2479 * @param vec coordinates on sphere
2481 static int flat_to_xyz(const V360Context *s,
2482 int i, int j, int width, int height,
2485 const float l_x = s->flat_range[0] * ((2.f * i + 0.5f) / width - 1.f);
2486 const float l_y = -s->flat_range[1] * ((2.f * j + 0.5f) / height - 1.f);
2492 normalize_vector(vec);
2498 * Prepare data for processing fisheye output format.
2500 * @param ctx filter context
2502 * @return error code
2504 static int prepare_fisheye_out(AVFilterContext *ctx)
2506 V360Context *s = ctx->priv;
2508 s->flat_range[0] = s->h_fov / 180.f;
2509 s->flat_range[1] = s->v_fov / 180.f;
2515 * Calculate 3D coordinates on sphere for corresponding frame position in fisheye format.
2517 * @param s filter private context
2518 * @param i horizontal position on frame [0, width)
2519 * @param j vertical position on frame [0, height)
2520 * @param width frame width
2521 * @param height frame height
2522 * @param vec coordinates on sphere
2524 static int fisheye_to_xyz(const V360Context *s,
2525 int i, int j, int width, int height,
2528 const float uf = s->flat_range[0] * ((2.f * i) / width - 1.f);
2529 const float vf = s->flat_range[1] * ((2.f * j + 1.f) / height - 1.f);
2531 const float phi = -atan2f(vf, uf);
2532 const float theta = -M_PI_2 * (1.f - hypotf(uf, vf));
2534 vec[0] = cosf(theta) * cosf(phi);
2535 vec[1] = cosf(theta) * sinf(phi);
2536 vec[2] = sinf(theta);
2538 normalize_vector(vec);
2544 * Prepare data for processing fisheye input format.
2546 * @param ctx filter context
2548 * @return error code
2550 static int prepare_fisheye_in(AVFilterContext *ctx)
2552 V360Context *s = ctx->priv;
2554 s->iflat_range[0] = s->ih_fov / 180.f;
2555 s->iflat_range[1] = s->iv_fov / 180.f;
2561 * Calculate frame position in fisheye format for corresponding 3D coordinates on sphere.
2563 * @param s filter private context
2564 * @param vec coordinates on sphere
2565 * @param width frame width
2566 * @param height frame height
2567 * @param us horizontal coordinates for interpolation window
2568 * @param vs vertical coordinates for interpolation window
2569 * @param du horizontal relative coordinate
2570 * @param dv vertical relative coordinate
2572 static int xyz_to_fisheye(const V360Context *s,
2573 const float *vec, int width, int height,
2574 int16_t us[4][4], int16_t vs[4][4], float *du, float *dv)
2576 const float h = hypotf(vec[0], vec[1]);
2577 const float lh = h > 0.f ? h : 1.f;
2578 const float phi = atan2f(h, -vec[2]) / M_PI;
2580 float uf = vec[0] / lh * phi * s->input_mirror_modifier[0] / s->iflat_range[0];
2581 float vf = -vec[1] / lh * phi * s->input_mirror_modifier[1] / s->iflat_range[1];
2583 const int visible = hypotf(uf, vf) <= 0.5f;
2586 uf = (uf + 0.5f) * width;
2587 vf = (vf + 0.5f) * height;
2592 *du = visible ? uf - ui : 0.f;
2593 *dv = visible ? vf - vi : 0.f;
2595 for (int i = 0; i < 4; i++) {
2596 for (int j = 0; j < 4; j++) {
2597 us[i][j] = visible ? av_clip(ui + j - 1, 0, width - 1) : 0;
2598 vs[i][j] = visible ? av_clip(vi + i - 1, 0, height - 1) : 0;
2606 * Calculate 3D coordinates on sphere for corresponding frame position in pannini format.
2608 * @param s filter private context
2609 * @param i horizontal position on frame [0, width)
2610 * @param j vertical position on frame [0, height)
2611 * @param width frame width
2612 * @param height frame height
2613 * @param vec coordinates on sphere
2615 static int pannini_to_xyz(const V360Context *s,
2616 int i, int j, int width, int height,
2619 const float uf = ((2.f * i + 1.f) / width - 1.f);
2620 const float vf = ((2.f * j + 1.f) / height - 1.f);
2622 const float d = s->h_fov;
2623 const float k = uf * uf / ((d + 1.f) * (d + 1.f));
2624 const float dscr = k * k * d * d - (k + 1.f) * (k * d * d - 1.f);
2625 const float clon = (-k * d + sqrtf(dscr)) / (k + 1.f);
2626 const float S = (d + 1.f) / (d + clon);
2627 const float lon = -(M_PI + atan2f(uf, S * clon));
2628 const float lat = -atan2f(vf, S);
2630 vec[0] = sinf(lon) * cosf(lat);
2632 vec[2] = cosf(lon) * cosf(lat);
2634 normalize_vector(vec);
2640 * Prepare data for processing cylindrical output format.
2642 * @param ctx filter context
2644 * @return error code
2646 static int prepare_cylindrical_out(AVFilterContext *ctx)
2648 V360Context *s = ctx->priv;
2650 s->flat_range[0] = M_PI * s->h_fov / 360.f;
2651 s->flat_range[1] = tanf(0.5f * s->v_fov * M_PI / 180.f);
2657 * Calculate 3D coordinates on sphere for corresponding frame position in cylindrical format.
2659 * @param s filter private context
2660 * @param i horizontal position on frame [0, width)
2661 * @param j vertical position on frame [0, height)
2662 * @param width frame width
2663 * @param height frame height
2664 * @param vec coordinates on sphere
2666 static int cylindrical_to_xyz(const V360Context *s,
2667 int i, int j, int width, int height,
2670 const float uf = s->flat_range[0] * ((2.f * i + 1.f) / width - 1.f);
2671 const float vf = s->flat_range[1] * ((2.f * j + 1.f) / height - 1.f);
2673 const float phi = uf;
2674 const float theta = atanf(vf);
2676 const float sin_phi = sinf(phi);
2677 const float cos_phi = cosf(phi);
2678 const float sin_theta = sinf(theta);
2679 const float cos_theta = cosf(theta);
2681 vec[0] = cos_theta * sin_phi;
2682 vec[1] = -sin_theta;
2683 vec[2] = -cos_theta * cos_phi;
2685 normalize_vector(vec);
2691 * Prepare data for processing cylindrical input format.
2693 * @param ctx filter context
2695 * @return error code
2697 static int prepare_cylindrical_in(AVFilterContext *ctx)
2699 V360Context *s = ctx->priv;
2701 s->iflat_range[0] = M_PI * s->ih_fov / 360.f;
2702 s->iflat_range[1] = tanf(0.5f * s->iv_fov * M_PI / 180.f);
2708 * Calculate frame position in cylindrical format for corresponding 3D coordinates on sphere.
2710 * @param s filter private context
2711 * @param vec coordinates on sphere
2712 * @param width frame width
2713 * @param height frame height
2714 * @param us horizontal coordinates for interpolation window
2715 * @param vs vertical coordinates for interpolation window
2716 * @param du horizontal relative coordinate
2717 * @param dv vertical relative coordinate
2719 static int xyz_to_cylindrical(const V360Context *s,
2720 const float *vec, int width, int height,
2721 int16_t us[4][4], int16_t vs[4][4], float *du, float *dv)
2723 const float phi = atan2f(vec[0], -vec[2]) * s->input_mirror_modifier[0] / s->iflat_range[0];
2724 const float theta = asinf(-vec[1]) * s->input_mirror_modifier[1];
2726 const float uf = (phi + 1.f) * (width - 1) / 2.f;
2727 const float vf = (tanf(theta) / s->iflat_range[1] + 1.f) * height / 2.f;
2729 const int ui = floorf(uf);
2730 const int vi = floorf(vf);
2732 const int visible = vi >= 0 && vi < height && ui >= 0 && ui < width &&
2733 theta <= M_PI * s->iv_fov / 180.f &&
2734 theta >= -M_PI * s->iv_fov / 180.f;
2739 for (int i = 0; i < 4; i++) {
2740 for (int j = 0; j < 4; j++) {
2741 us[i][j] = visible ? av_clip(ui + j - 1, 0, width - 1) : 0;
2742 vs[i][j] = visible ? av_clip(vi + i - 1, 0, height - 1) : 0;
2750 * Calculate 3D coordinates on sphere for corresponding frame position in perspective format.
2752 * @param s filter private context
2753 * @param i horizontal position on frame [0, width)
2754 * @param j vertical position on frame [0, height)
2755 * @param width frame width
2756 * @param height frame height
2757 * @param vec coordinates on sphere
2759 static int perspective_to_xyz(const V360Context *s,
2760 int i, int j, int width, int height,
2763 const float uf = ((2.f * i + 1.f) / width - 1.f);
2764 const float vf = ((2.f * j + 1.f) / height - 1.f);
2765 const float rh = hypotf(uf, vf);
2766 const float sinzz = 1.f - rh * rh;
2767 const float h = 1.f + s->v_fov;
2768 const float sinz = (h - sqrtf(sinzz)) / (h / rh + rh / h);
2769 const float sinz2 = sinz * sinz;
2772 const float cosz = sqrtf(1.f - sinz2);
2774 const float theta = asinf(cosz);
2775 const float phi = atan2f(uf, vf);
2777 const float sin_phi = sinf(phi);
2778 const float cos_phi = cosf(phi);
2779 const float sin_theta = sinf(theta);
2780 const float cos_theta = cosf(theta);
2782 vec[0] = cos_theta * sin_phi;
2784 vec[2] = -cos_theta * cos_phi;
2792 normalize_vector(vec);
2797 * Calculate 3D coordinates on sphere for corresponding frame position in tetrahedron format.
2799 * @param s filter private context
2800 * @param i horizontal position on frame [0, width)
2801 * @param j vertical position on frame [0, height)
2802 * @param width frame width
2803 * @param height frame height
2804 * @param vec coordinates on sphere
2806 static int tetrahedron_to_xyz(const V360Context *s,
2807 int i, int j, int width, int height,
2810 const float uf = (float)i / width;
2811 const float vf = (float)j / height;
2813 vec[0] = uf < 0.5f ? uf * 4.f - 1.f : 3.f - uf * 4.f;
2814 vec[1] = 1.f - vf * 2.f;
2815 vec[2] = 2.f * fabsf(1.f - fabsf(1.f - uf * 2.f + vf)) - 1.f;
2817 normalize_vector(vec);
2823 * Calculate frame position in tetrahedron format for corresponding 3D coordinates on sphere.
2825 * @param s filter private context
2826 * @param vec coordinates on sphere
2827 * @param width frame width
2828 * @param height frame height
2829 * @param us horizontal coordinates for interpolation window
2830 * @param vs vertical coordinates for interpolation window
2831 * @param du horizontal relative coordinate
2832 * @param dv vertical relative coordinate
2834 static int xyz_to_tetrahedron(const V360Context *s,
2835 const float *vec, int width, int height,
2836 int16_t us[4][4], int16_t vs[4][4], float *du, float *dv)
2838 const float d0 = vec[0] * 1.f + vec[1] * 1.f + vec[2] *-1.f;
2839 const float d1 = vec[0] *-1.f + vec[1] *-1.f + vec[2] *-1.f;
2840 const float d2 = vec[0] * 1.f + vec[1] *-1.f + vec[2] * 1.f;
2841 const float d3 = vec[0] *-1.f + vec[1] * 1.f + vec[2] * 1.f;
2842 const float d = FFMAX(d0, FFMAX3(d1, d2, d3));
2844 float uf, vf, x, y, z;
2851 vf = 0.5f - y * 0.5f * s->input_mirror_modifier[1];
2853 if ((x + y >= 0.f && y + z >= 0.f && -z - x <= 0.f) ||
2854 (x + y <= 0.f && -y + z >= 0.f && z - x >= 0.f)) {
2855 uf = 0.25f * x * s->input_mirror_modifier[0] + 0.25f;
2857 uf = 0.75f - 0.25f * x * s->input_mirror_modifier[0];
2869 for (int i = 0; i < 4; i++) {
2870 for (int j = 0; j < 4; j++) {
2871 us[i][j] = reflectx(ui + j - 1, vi + i - 1, width, height);
2872 vs[i][j] = reflecty(vi + i - 1, height);
2880 * Calculate 3D coordinates on sphere for corresponding frame position in dual fisheye format.
2882 * @param s filter private context
2883 * @param i horizontal position on frame [0, width)
2884 * @param j vertical position on frame [0, height)
2885 * @param width frame width
2886 * @param height frame height
2887 * @param vec coordinates on sphere
2889 static int dfisheye_to_xyz(const V360Context *s,
2890 int i, int j, int width, int height,
2893 const float scale = 1.f + s->out_pad;
2895 const float ew = width / 2.f;
2896 const float eh = height;
2898 const int ei = i >= ew ? i - ew : i;
2899 const float m = i >= ew ? -1.f : 1.f;
2901 const float uf = ((2.f * ei) / ew - 1.f) * scale;
2902 const float vf = ((2.f * j + 1.f) / eh - 1.f) * scale;
2904 const float h = hypotf(uf, vf);
2905 const float lh = h > 0.f ? h : 1.f;
2906 const float theta = m * M_PI_2 * (1.f - h);
2908 const float sin_theta = sinf(theta);
2909 const float cos_theta = cosf(theta);
2911 vec[0] = cos_theta * m * -uf / lh;
2912 vec[1] = cos_theta * -vf / lh;
2915 normalize_vector(vec);
2921 * Calculate frame position in dual fisheye format for corresponding 3D coordinates on sphere.
2923 * @param s filter private context
2924 * @param vec coordinates on sphere
2925 * @param width frame width
2926 * @param height frame height
2927 * @param us horizontal coordinates for interpolation window
2928 * @param vs vertical coordinates for interpolation window
2929 * @param du horizontal relative coordinate
2930 * @param dv vertical relative coordinate
2932 static int xyz_to_dfisheye(const V360Context *s,
2933 const float *vec, int width, int height,
2934 int16_t us[4][4], int16_t vs[4][4], float *du, float *dv)
2936 const float scale = 1.f - s->in_pad;
2938 const float ew = width / 2.f;
2939 const float eh = height;
2941 const float h = hypotf(vec[0], vec[1]);
2942 const float lh = h > 0.f ? h : 1.f;
2943 const float theta = acosf(fabsf(vec[2])) / M_PI;
2945 float uf = (theta * (-vec[0] / lh) * s->input_mirror_modifier[0] * scale + 0.5f) * ew;
2946 float vf = (theta * (-vec[1] / lh) * s->input_mirror_modifier[1] * scale + 0.5f) * eh;
2951 if (vec[2] >= 0.f) {
2954 u_shift = ceilf(ew);
2964 for (int i = 0; i < 4; i++) {
2965 for (int j = 0; j < 4; j++) {
2966 us[i][j] = av_clip(u_shift + ui + j - 1, 0, width - 1);
2967 vs[i][j] = av_clip( vi + i - 1, 0, height - 1);
2975 * Calculate 3D coordinates on sphere for corresponding frame position in barrel facebook's format.
2977 * @param s filter private context
2978 * @param i horizontal position on frame [0, width)
2979 * @param j vertical position on frame [0, height)
2980 * @param width frame width
2981 * @param height frame height
2982 * @param vec coordinates on sphere
2984 static int barrel_to_xyz(const V360Context *s,
2985 int i, int j, int width, int height,
2988 const float scale = 0.99f;
2989 float l_x, l_y, l_z;
2991 if (i < 4 * width / 5) {
2992 const float theta_range = M_PI_4;
2994 const int ew = 4 * width / 5;
2995 const int eh = height;
2997 const float phi = ((2.f * i) / ew - 1.f) * M_PI / scale;
2998 const float theta = ((2.f * j) / eh - 1.f) * theta_range / scale;
3000 const float sin_phi = sinf(phi);
3001 const float cos_phi = cosf(phi);
3002 const float sin_theta = sinf(theta);
3003 const float cos_theta = cosf(theta);
3005 l_x = cos_theta * sin_phi;
3007 l_z = -cos_theta * cos_phi;
3009 const int ew = width / 5;
3010 const int eh = height / 2;
3015 uf = 2.f * (i - 4 * ew) / ew - 1.f;
3016 vf = 2.f * (j ) / eh - 1.f;
3025 uf = 2.f * (i - 4 * ew) / ew - 1.f;
3026 vf = 2.f * (j - eh) / eh - 1.f;
3041 normalize_vector(vec);
3047 * Calculate frame position in barrel facebook's format for corresponding 3D coordinates on sphere.
3049 * @param s filter private context
3050 * @param vec coordinates on sphere
3051 * @param width frame width
3052 * @param height frame height
3053 * @param us horizontal coordinates for interpolation window
3054 * @param vs vertical coordinates for interpolation window
3055 * @param du horizontal relative coordinate
3056 * @param dv vertical relative coordinate
3058 static int xyz_to_barrel(const V360Context *s,
3059 const float *vec, int width, int height,
3060 int16_t us[4][4], int16_t vs[4][4], float *du, float *dv)
3062 const float scale = 0.99f;
3064 const float phi = atan2f(vec[0], -vec[2]) * s->input_mirror_modifier[0];
3065 const float theta = asinf(-vec[1]) * s->input_mirror_modifier[1];
3066 const float theta_range = M_PI_4;
3069 int u_shift, v_shift;
3073 if (theta > -theta_range && theta < theta_range) {
3077 u_shift = s->ih_flip ? width / 5 : 0;
3080 uf = (phi / M_PI * scale + 1.f) * ew / 2.f;
3081 vf = (theta / theta_range * scale + 1.f) * eh / 2.f;
3086 u_shift = s->ih_flip ? 0 : 4 * ew;
3088 if (theta < 0.f) { // UP
3089 uf = vec[0] / vec[1];
3090 vf = -vec[2] / vec[1];
3093 uf = -vec[0] / vec[1];
3094 vf = -vec[2] / vec[1];
3098 uf *= s->input_mirror_modifier[0] * s->input_mirror_modifier[1];
3099 vf *= s->input_mirror_modifier[1];
3101 uf = 0.5f * ew * (uf * scale + 1.f);
3102 vf = 0.5f * eh * (vf * scale + 1.f);
3111 for (int i = 0; i < 4; i++) {
3112 for (int j = 0; j < 4; j++) {
3113 us[i][j] = u_shift + av_clip(ui + j - 1, 0, ew - 1);
3114 vs[i][j] = v_shift + av_clip(vi + i - 1, 0, eh - 1);
3122 * Calculate frame position in barrel split facebook's format for corresponding 3D coordinates on sphere.
3124 * @param s filter private context
3125 * @param vec coordinates on sphere
3126 * @param width frame width
3127 * @param height frame height
3128 * @param us horizontal coordinates for interpolation window
3129 * @param vs vertical coordinates for interpolation window
3130 * @param du horizontal relative coordinate
3131 * @param dv vertical relative coordinate
3133 static int xyz_to_barrelsplit(const V360Context *s,
3134 const float *vec, int width, int height,
3135 int16_t us[4][4], int16_t vs[4][4], float *du, float *dv)
3137 const float phi = atan2f(vec[0], -vec[2]) * s->input_mirror_modifier[0];
3138 const float theta = asinf(-vec[1]) * s->input_mirror_modifier[1];
3140 const float theta_range = M_PI_4;
3143 int u_shift, v_shift;
3147 if (theta >= -theta_range && theta <= theta_range) {
3148 const float scalew = s->fin_pad > 0 ? 1.f - s->fin_pad / (width * 2.f / 3.f) : 1.f - s->in_pad;
3149 const float scaleh = s->fin_pad > 0 ? 1.f - s->fin_pad / (height / 2.f) : 1.f - s->in_pad;
3154 u_shift = s->ih_flip ? width / 3 : 0;
3155 v_shift = phi >= M_PI_2 || phi < -M_PI_2 ? eh : 0;
3157 uf = fmodf(phi, M_PI_2) / M_PI_2;
3158 vf = theta / M_PI_4;
3161 uf = uf >= 0.f ? fmodf(uf - 1.f, 1.f) : fmodf(uf + 1.f, 1.f);
3163 uf = (uf * scalew + 1.f) * width / 3.f;
3164 vf = (vf * scaleh + 1.f) * height / 4.f;
3166 const float scalew = s->fin_pad > 0 ? 1.f - s->fin_pad / (width / 3.f) : 1.f - s->in_pad;
3167 const float scaleh = s->fin_pad > 0 ? 1.f - s->fin_pad / (height / 4.f) : 1.f - s->in_pad;
3173 u_shift = s->ih_flip ? 0 : 2 * ew;
3175 if (theta <= 0.f && theta >= -M_PI_2 &&
3176 phi <= M_PI_2 && phi >= -M_PI_2) {
3177 uf = vec[0] / vec[1];
3178 vf = -vec[2] / vec[1];
3181 } else if (theta >= 0.f && theta <= M_PI_2 &&
3182 phi <= M_PI_2 && phi >= -M_PI_2) {
3183 uf = -vec[0] / vec[1];
3184 vf = -vec[2] / vec[1];
3185 v_shift = height * 0.25f;
3186 } else if (theta <= 0.f && theta >= -M_PI_2) {
3187 uf = -vec[0] / vec[1];
3188 vf = vec[2] / vec[1];
3189 v_shift = height * 0.5f;
3192 uf = vec[0] / vec[1];
3193 vf = vec[2] / vec[1];
3194 v_shift = height * 0.75f;
3197 uf *= s->input_mirror_modifier[0] * s->input_mirror_modifier[1];
3198 vf *= s->input_mirror_modifier[1];
3200 uf = 0.5f * width / 3.f * (uf * scalew + 1.f);
3201 vf = height * 0.25f * (vf * scaleh + 1.f) + v_offset;
3210 for (int i = 0; i < 4; i++) {
3211 for (int j = 0; j < 4; j++) {
3212 us[i][j] = u_shift + av_clip(ui + j - 1, 0, ew - 1);
3213 vs[i][j] = v_shift + av_clip(vi + i - 1, 0, eh - 1);
3221 * Calculate 3D coordinates on sphere for corresponding frame position in barrel split facebook's format.
3223 * @param s filter private context
3224 * @param i horizontal position on frame [0, width)
3225 * @param j vertical position on frame [0, height)
3226 * @param width frame width
3227 * @param height frame height
3228 * @param vec coordinates on sphere
3230 static int barrelsplit_to_xyz(const V360Context *s,
3231 int i, int j, int width, int height,
3234 const float x = (i + 0.5f) / width;
3235 const float y = (j + 0.5f) / height;
3236 float l_x, l_y, l_z;
3238 if (x < 2.f / 3.f) {
3239 const float scalew = s->fout_pad > 0 ? 1.f - s->fout_pad / (width * 2.f / 3.f) : 1.f - s->out_pad;
3240 const float scaleh = s->fout_pad > 0 ? 1.f - s->fout_pad / (height / 2.f) : 1.f - s->out_pad;
3242 const float back = floorf(y * 2.f);
3244 const float phi = ((3.f / 2.f * x - 0.5f) / scalew - back + 1.f) * M_PI;
3245 const float theta = (y - 0.25f - 0.5f * back) / scaleh * M_PI;
3247 const float sin_phi = sinf(phi);
3248 const float cos_phi = cosf(phi);
3249 const float sin_theta = sinf(theta);
3250 const float cos_theta = cosf(theta);
3252 l_x = -cos_theta * sin_phi;
3254 l_z = cos_theta * cos_phi;
3256 const float scalew = s->fout_pad > 0 ? 1.f - s->fout_pad / (width / 3.f) : 1.f - s->out_pad;
3257 const float scaleh = s->fout_pad > 0 ? 1.f - s->fout_pad / (height / 4.f) : 1.f - s->out_pad;
3259 const int face = floorf(y * 4.f);
3270 l_x = (0.5f - uf) / scalew;
3272 l_z = (-0.5f + vf) / scaleh;
3277 vf = 1.f - (vf - 0.5f);
3279 l_x = (0.5f - uf) / scalew;
3281 l_z = (0.5f - vf) / scaleh;
3284 vf = y * 2.f - 0.5f;
3285 vf = 1.f - (1.f - vf);
3287 l_x = (0.5f - uf) / scalew;
3289 l_z = (-0.5f + vf) / scaleh;
3292 vf = y * 2.f - 1.5f;
3294 l_x = (0.5f - uf) / scalew;
3296 l_z = (0.5f - vf) / scaleh;
3305 normalize_vector(vec);
3311 * Calculate 3D coordinates on sphere for corresponding frame position in tspyramid format.
3313 * @param s filter private context
3314 * @param i horizontal position on frame [0, width)
3315 * @param j vertical position on frame [0, height)
3316 * @param width frame width
3317 * @param height frame height
3318 * @param vec coordinates on sphere
3320 static int tspyramid_to_xyz(const V360Context *s,
3321 int i, int j, int width, int height,
3324 const float x = (i + 0.5f) / width;
3325 const float y = (j + 0.5f) / height;
3328 vec[0] = x * 4.f - 1.f;
3329 vec[1] = -(y * 2.f - 1.f);
3331 } else if (x >= 0.6875f && x < 0.8125f &&
3332 y >= 0.375f && y < 0.625f) {
3333 vec[0] = -(x - 0.6875f) * 16.f + 1.f;
3334 vec[1] = -(y - 0.375f) * 8.f + 1.f;
3336 } else if (0.5f <= x && x < 0.6875f &&
3337 ((0.f <= y && y < 0.375f && y >= 2.f * (x - 0.5f)) ||
3338 (0.375f <= y && y < 0.625f) ||
3339 (0.625f <= y && y < 1.f && y <= 2.f * (1.f - x)))) {
3341 vec[1] = -2.f * (y - 2.f * x + 1.f) / (3.f - 4.f * x) + 1.f;
3342 vec[2] = 2.f * (x - 0.5f) / 0.1875f - 1.f;
3343 } else if (0.8125f <= x && x < 1.f &&
3344 ((0.f <= y && y < 0.375f && x >= (1.f - y / 2.f)) ||
3345 (0.375f <= y && y < 0.625f) ||
3346 (0.625f <= y && y < 1.f && y <= (2.f * x - 1.f)))) {
3348 vec[1] = -2.f * (y + 2.f * x - 2.f) / (4.f * x - 3.f) + 1.f;
3349 vec[2] = -2.f * (x - 0.8125f) / 0.1875f + 1.f;
3350 } else if (0.f <= y && y < 0.375f &&
3351 ((0.5f <= x && x < 0.8125f && y < 2.f * (x - 0.5f)) ||
3352 (0.6875f <= x && x < 0.8125f) ||
3353 (0.8125f <= x && x < 1.f && x < (1.f - y / 2.f)))) {
3354 vec[0] = 2.f * (1.f - x - 0.5f * y) / (0.5f - y) - 1.f;
3356 vec[2] = -2.f * (0.375f - y) / 0.375f + 1.f;
3358 vec[0] = 2.f * (0.5f - x + 0.5f * y) / (y - 0.5f) - 1.f;
3360 vec[2] = 2.f * (1.f - y) / 0.375f - 1.f;
3363 normalize_vector(vec);
3369 * Calculate frame position in tspyramid format for corresponding 3D coordinates on sphere.
3371 * @param s filter private context
3372 * @param vec coordinates on sphere
3373 * @param width frame width
3374 * @param height frame height
3375 * @param us horizontal coordinates for interpolation window
3376 * @param vs vertical coordinates for interpolation window
3377 * @param du horizontal relative coordinate
3378 * @param dv vertical relative coordinate
3380 static int xyz_to_tspyramid(const V360Context *s,
3381 const float *vec, int width, int height,
3382 int16_t us[4][4], int16_t vs[4][4], float *du, float *dv)
3388 xyz_to_cube(s, vec, &uf, &vf, &face);
3390 uf = (uf + 1.f) * 0.5f;
3391 vf = (vf + 1.f) * 0.5f;
3395 uf = 0.1875f * vf - 0.375f * uf * vf - 0.125f * uf + 0.8125f;
3396 vf = 0.375f - 0.375f * vf;
3402 uf = 1.f - 0.1875f * vf - 0.5f * uf + 0.375f * uf * vf;
3403 vf = 1.f - 0.375f * vf;
3406 vf = 0.25f * vf + 0.75f * uf * vf - 0.375f * uf + 0.375f;
3407 uf = 0.1875f * uf + 0.8125f;
3410 vf = 0.375f * uf - 0.75f * uf * vf + vf;
3411 uf = 0.1875f * uf + 0.5f;
3414 uf = 0.125f * uf + 0.6875f;
3415 vf = 0.25f * vf + 0.375f;
3428 for (int i = 0; i < 4; i++) {
3429 for (int j = 0; j < 4; j++) {
3430 us[i][j] = reflectx(ui + j - 1, vi + i - 1, width, height);
3431 vs[i][j] = reflecty(vi + i - 1, height);
3438 static void multiply_matrix(float c[3][3], const float a[3][3], const float b[3][3])
3440 for (int i = 0; i < 3; i++) {
3441 for (int j = 0; j < 3; j++) {
3444 for (int k = 0; k < 3; k++)
3445 sum += a[i][k] * b[k][j];
3453 * Calculate rotation matrix for yaw/pitch/roll angles.
3455 static inline void calculate_rotation_matrix(float yaw, float pitch, float roll,
3456 float rot_mat[3][3],
3457 const int rotation_order[3])
3459 const float yaw_rad = yaw * M_PI / 180.f;
3460 const float pitch_rad = pitch * M_PI / 180.f;
3461 const float roll_rad = roll * M_PI / 180.f;
3463 const float sin_yaw = sinf(-yaw_rad);
3464 const float cos_yaw = cosf(-yaw_rad);
3465 const float sin_pitch = sinf(pitch_rad);
3466 const float cos_pitch = cosf(pitch_rad);
3467 const float sin_roll = sinf(roll_rad);
3468 const float cos_roll = cosf(roll_rad);
3473 m[0][0][0] = cos_yaw; m[0][0][1] = 0; m[0][0][2] = sin_yaw;
3474 m[0][1][0] = 0; m[0][1][1] = 1; m[0][1][2] = 0;
3475 m[0][2][0] = -sin_yaw; m[0][2][1] = 0; m[0][2][2] = cos_yaw;
3477 m[1][0][0] = 1; m[1][0][1] = 0; m[1][0][2] = 0;
3478 m[1][1][0] = 0; m[1][1][1] = cos_pitch; m[1][1][2] = -sin_pitch;
3479 m[1][2][0] = 0; m[1][2][1] = sin_pitch; m[1][2][2] = cos_pitch;
3481 m[2][0][0] = cos_roll; m[2][0][1] = -sin_roll; m[2][0][2] = 0;
3482 m[2][1][0] = sin_roll; m[2][1][1] = cos_roll; m[2][1][2] = 0;
3483 m[2][2][0] = 0; m[2][2][1] = 0; m[2][2][2] = 1;
3485 multiply_matrix(temp, m[rotation_order[0]], m[rotation_order[1]]);
3486 multiply_matrix(rot_mat, temp, m[rotation_order[2]]);
3490 * Rotate vector with given rotation matrix.
3492 * @param rot_mat rotation matrix
3495 static inline void rotate(const float rot_mat[3][3],
3498 const float x_tmp = vec[0] * rot_mat[0][0] + vec[1] * rot_mat[0][1] + vec[2] * rot_mat[0][2];
3499 const float y_tmp = vec[0] * rot_mat[1][0] + vec[1] * rot_mat[1][1] + vec[2] * rot_mat[1][2];
3500 const float z_tmp = vec[0] * rot_mat[2][0] + vec[1] * rot_mat[2][1] + vec[2] * rot_mat[2][2];
3507 static inline void set_mirror_modifier(int h_flip, int v_flip, int d_flip,
3510 modifier[0] = h_flip ? -1.f : 1.f;
3511 modifier[1] = v_flip ? -1.f : 1.f;
3512 modifier[2] = d_flip ? -1.f : 1.f;
3515 static inline void mirror(const float *modifier, float *vec)
3517 vec[0] *= modifier[0];
3518 vec[1] *= modifier[1];
3519 vec[2] *= modifier[2];
3522 static int allocate_plane(V360Context *s, int sizeof_uv, int sizeof_ker, int sizeof_mask, int p)
3525 s->u[p] = av_calloc(s->uv_linesize[p] * s->pr_height[p], sizeof_uv);
3527 s->v[p] = av_calloc(s->uv_linesize[p] * s->pr_height[p], sizeof_uv);
3528 if (!s->u[p] || !s->v[p])
3529 return AVERROR(ENOMEM);
3532 s->ker[p] = av_calloc(s->uv_linesize[p] * s->pr_height[p], sizeof_ker);
3534 return AVERROR(ENOMEM);
3537 if (sizeof_mask && !p) {
3539 s->mask = av_calloc(s->pr_width[p] * s->pr_height[p], sizeof_mask);
3541 return AVERROR(ENOMEM);
3547 static void fov_from_dfov(int format, float d_fov, float w, float h, float *h_fov, float *v_fov)
3552 const float d = 0.5f * hypotf(w, h);
3553 const float l = d / (tanf(d_fov * M_PI / 720.f));
3555 *h_fov = 2.f * atan2f(w * 0.5f, l) * 360.f / M_PI;
3556 *v_fov = 2.f * atan2f(h * 0.5f, l) * 360.f / M_PI;
3561 const float d = 0.5f * hypotf(w, h);
3563 *h_fov = d / h * d_fov;
3564 *v_fov = d / w * d_fov;
3570 const float da = tanf(0.5f * FFMIN(d_fov, 359.f) * M_PI / 180.f);
3571 const float d = hypotf(w, h);
3573 *h_fov = atan2f(da * w, d) * 360.f / M_PI;
3574 *v_fov = atan2f(da * h, d) * 360.f / M_PI;
3585 static void set_dimensions(int *outw, int *outh, int w, int h, const AVPixFmtDescriptor *desc)
3587 outw[1] = outw[2] = FF_CEIL_RSHIFT(w, desc->log2_chroma_w);
3588 outw[0] = outw[3] = w;
3589 outh[1] = outh[2] = FF_CEIL_RSHIFT(h, desc->log2_chroma_h);
3590 outh[0] = outh[3] = h;
3593 // Calculate remap data
3594 static av_always_inline int v360_slice(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
3596 V360Context *s = ctx->priv;
3598 for (int p = 0; p < s->nb_allocated; p++) {
3599 const int max_value = s->max_value;
3600 const int width = s->pr_width[p];
3601 const int uv_linesize = s->uv_linesize[p];
3602 const int height = s->pr_height[p];
3603 const int in_width = s->inplanewidth[p];
3604 const int in_height = s->inplaneheight[p];
3605 const int slice_start = (height * jobnr ) / nb_jobs;
3606 const int slice_end = (height * (jobnr + 1)) / nb_jobs;
3611 for (int j = slice_start; j < slice_end; j++) {
3612 for (int i = 0; i < width; i++) {
3613 int16_t *u = s->u[p] + (j * uv_linesize + i) * s->elements;
3614 int16_t *v = s->v[p] + (j * uv_linesize + i) * s->elements;
3615 int16_t *ker = s->ker[p] + (j * uv_linesize + i) * s->elements;
3616 uint8_t *mask8 = p ? NULL : s->mask + (j * s->pr_width[0] + i);
3617 uint16_t *mask16 = p ? NULL : (uint16_t *)s->mask + (j * s->pr_width[0] + i);
3618 int in_mask, out_mask;
3620 if (s->out_transpose)
3621 out_mask = s->out_transform(s, j, i, height, width, vec);
3623 out_mask = s->out_transform(s, i, j, width, height, vec);
3624 av_assert1(!isnan(vec[0]) && !isnan(vec[1]) && !isnan(vec[2]));
3625 rotate(s->rot_mat, vec);
3626 av_assert1(!isnan(vec[0]) && !isnan(vec[1]) && !isnan(vec[2]));
3627 normalize_vector(vec);
3628 mirror(s->output_mirror_modifier, vec);
3629 if (s->in_transpose)
3630 in_mask = s->in_transform(s, vec, in_height, in_width, rmap.v, rmap.u, &du, &dv);
3632 in_mask = s->in_transform(s, vec, in_width, in_height, rmap.u, rmap.v, &du, &dv);
3633 av_assert1(!isnan(du) && !isnan(dv));
3634 s->calculate_kernel(du, dv, &rmap, u, v, ker);
3636 if (!p && s->mask) {
3637 if (s->mask_size == 1) {
3638 mask8[0] = 255 * (out_mask & in_mask);
3640 mask16[0] = max_value * (out_mask & in_mask);
3650 static int config_output(AVFilterLink *outlink)
3652 AVFilterContext *ctx = outlink->src;
3653 AVFilterLink *inlink = ctx->inputs[0];
3654 V360Context *s = ctx->priv;
3655 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(inlink->format);
3656 const int depth = desc->comp[0].depth;
3657 const int sizeof_mask = s->mask_size = (depth + 7) >> 3;
3662 int in_offset_h, in_offset_w;
3663 int out_offset_h, out_offset_w;
3665 int (*prepare_out)(AVFilterContext *ctx);
3668 s->max_value = (1 << depth) - 1;
3669 s->input_mirror_modifier[0] = s->ih_flip ? -1.f : 1.f;
3670 s->input_mirror_modifier[1] = s->iv_flip ? -1.f : 1.f;
3672 switch (s->interp) {
3674 s->calculate_kernel = nearest_kernel;
3675 s->remap_slice = depth <= 8 ? remap1_8bit_slice : remap1_16bit_slice;
3677 sizeof_uv = sizeof(int16_t) * s->elements;
3681 s->calculate_kernel = bilinear_kernel;
3682 s->remap_slice = depth <= 8 ? remap2_8bit_slice : remap2_16bit_slice;
3683 s->elements = 2 * 2;
3684 sizeof_uv = sizeof(int16_t) * s->elements;
3685 sizeof_ker = sizeof(int16_t) * s->elements;
3688 s->calculate_kernel = bicubic_kernel;
3689 s->remap_slice = depth <= 8 ? remap4_8bit_slice : remap4_16bit_slice;
3690 s->elements = 4 * 4;
3691 sizeof_uv = sizeof(int16_t) * s->elements;
3692 sizeof_ker = sizeof(int16_t) * s->elements;
3695 s->calculate_kernel = lanczos_kernel;
3696 s->remap_slice = depth <= 8 ? remap4_8bit_slice : remap4_16bit_slice;
3697 s->elements = 4 * 4;
3698 sizeof_uv = sizeof(int16_t) * s->elements;
3699 sizeof_ker = sizeof(int16_t) * s->elements;
3702 s->calculate_kernel = spline16_kernel;
3703 s->remap_slice = depth <= 8 ? remap4_8bit_slice : remap4_16bit_slice;
3704 s->elements = 4 * 4;
3705 sizeof_uv = sizeof(int16_t) * s->elements;
3706 sizeof_ker = sizeof(int16_t) * s->elements;
3709 s->calculate_kernel = gaussian_kernel;
3710 s->remap_slice = depth <= 8 ? remap4_8bit_slice : remap4_16bit_slice;
3711 s->elements = 4 * 4;
3712 sizeof_uv = sizeof(int16_t) * s->elements;
3713 sizeof_ker = sizeof(int16_t) * s->elements;
3719 ff_v360_init(s, depth);
3721 for (int order = 0; order < NB_RORDERS; order++) {
3722 const char c = s->rorder[order];
3726 av_log(ctx, AV_LOG_WARNING,
3727 "Incomplete rorder option. Direction for all 3 rotation orders should be specified. Switching to default rorder.\n");
3728 s->rotation_order[0] = YAW;
3729 s->rotation_order[1] = PITCH;
3730 s->rotation_order[2] = ROLL;
3734 rorder = get_rorder(c);
3736 av_log(ctx, AV_LOG_WARNING,
3737 "Incorrect rotation order symbol '%c' in rorder option. Switching to default rorder.\n", c);
3738 s->rotation_order[0] = YAW;
3739 s->rotation_order[1] = PITCH;
3740 s->rotation_order[2] = ROLL;
3744 s->rotation_order[order] = rorder;
3747 switch (s->in_stereo) {
3751 in_offset_w = in_offset_h = 0;
3769 set_dimensions(s->inplanewidth, s->inplaneheight, w, h, desc);
3770 set_dimensions(s->in_offset_w, s->in_offset_h, in_offset_w, in_offset_h, desc);
3772 s->in_width = s->inplanewidth[0];
3773 s->in_height = s->inplaneheight[0];
3775 if (s->id_fov > 0.f)
3776 fov_from_dfov(s->in, s->id_fov, w, h, &s->ih_fov, &s->iv_fov);
3778 if (s->in_transpose)
3779 FFSWAP(int, s->in_width, s->in_height);
3782 case EQUIRECTANGULAR:
3783 s->in_transform = xyz_to_equirect;
3789 s->in_transform = xyz_to_cube3x2;
3790 err = prepare_cube_in(ctx);
3795 s->in_transform = xyz_to_cube1x6;
3796 err = prepare_cube_in(ctx);
3801 s->in_transform = xyz_to_cube6x1;
3802 err = prepare_cube_in(ctx);
3807 s->in_transform = xyz_to_eac;
3808 err = prepare_eac_in(ctx);
3813 s->in_transform = xyz_to_flat;
3814 err = prepare_flat_in(ctx);
3820 av_log(ctx, AV_LOG_ERROR, "Supplied format is not accepted as input.\n");
3821 return AVERROR(EINVAL);
3823 s->in_transform = xyz_to_dfisheye;
3829 s->in_transform = xyz_to_barrel;
3835 s->in_transform = xyz_to_stereographic;
3836 err = prepare_stereographic_in(ctx);
3841 s->in_transform = xyz_to_mercator;
3847 s->in_transform = xyz_to_ball;
3853 s->in_transform = xyz_to_hammer;
3859 s->in_transform = xyz_to_sinusoidal;
3865 s->in_transform = xyz_to_fisheye;
3866 err = prepare_fisheye_in(ctx);
3871 s->in_transform = xyz_to_cylindrical;
3872 err = prepare_cylindrical_in(ctx);
3877 s->in_transform = xyz_to_tetrahedron;
3883 s->in_transform = xyz_to_barrelsplit;
3889 s->in_transform = xyz_to_tspyramid;
3894 case HEQUIRECTANGULAR:
3895 s->in_transform = xyz_to_hequirect;
3901 av_log(ctx, AV_LOG_ERROR, "Specified input format is not handled.\n");
3910 case EQUIRECTANGULAR:
3911 s->out_transform = equirect_to_xyz;
3917 s->out_transform = cube3x2_to_xyz;
3918 prepare_out = prepare_cube_out;
3919 w = lrintf(wf / 4.f * 3.f);
3923 s->out_transform = cube1x6_to_xyz;
3924 prepare_out = prepare_cube_out;
3925 w = lrintf(wf / 4.f);
3926 h = lrintf(hf * 3.f);
3929 s->out_transform = cube6x1_to_xyz;
3930 prepare_out = prepare_cube_out;
3931 w = lrintf(wf / 2.f * 3.f);
3932 h = lrintf(hf / 2.f);
3935 s->out_transform = eac_to_xyz;
3936 prepare_out = prepare_eac_out;
3938 h = lrintf(hf / 8.f * 9.f);
3941 s->out_transform = flat_to_xyz;
3942 prepare_out = prepare_flat_out;
3947 s->out_transform = dfisheye_to_xyz;
3953 s->out_transform = barrel_to_xyz;
3955 w = lrintf(wf / 4.f * 5.f);
3959 s->out_transform = stereographic_to_xyz;
3960 prepare_out = prepare_stereographic_out;
3962 h = lrintf(hf * 2.f);
3965 s->out_transform = mercator_to_xyz;
3968 h = lrintf(hf * 2.f);
3971 s->out_transform = ball_to_xyz;
3974 h = lrintf(hf * 2.f);
3977 s->out_transform = hammer_to_xyz;
3983 s->out_transform = sinusoidal_to_xyz;
3989 s->out_transform = fisheye_to_xyz;
3990 prepare_out = prepare_fisheye_out;
3991 w = lrintf(wf * 0.5f);
3995 s->out_transform = pannini_to_xyz;
4001 s->out_transform = cylindrical_to_xyz;
4002 prepare_out = prepare_cylindrical_out;
4004 h = lrintf(hf * 0.5f);
4007 s->out_transform = perspective_to_xyz;
4009 w = lrintf(wf / 2.f);
4013 s->out_transform = tetrahedron_to_xyz;
4019 s->out_transform = barrelsplit_to_xyz;
4021 w = lrintf(wf / 4.f * 3.f);
4025 s->out_transform = tspyramid_to_xyz;
4030 case HEQUIRECTANGULAR:
4031 s->out_transform = hequirect_to_xyz;
4033 w = lrintf(wf / 2.f);
4037 av_log(ctx, AV_LOG_ERROR, "Specified output format is not handled.\n");
4041 // Override resolution with user values if specified
4042 if (s->width > 0 && s->height <= 0 && s->h_fov > 0.f && s->v_fov > 0.f &&
4043 s->out == FLAT && s->d_fov == 0.f) {
4045 h = w / tanf(s->h_fov * M_PI / 360.f) * tanf(s->v_fov * M_PI / 360.f);
4046 } else if (s->width <= 0 && s->height > 0 && s->h_fov > 0.f && s->v_fov > 0.f &&
4047 s->out == FLAT && s->d_fov == 0.f) {
4049 w = h / tanf(s->v_fov * M_PI / 360.f) * tanf(s->h_fov * M_PI / 360.f);
4050 } else if (s->width > 0 && s->height > 0) {
4053 } else if (s->width > 0 || s->height > 0) {
4054 av_log(ctx, AV_LOG_ERROR, "Both width and height values should be specified.\n");
4055 return AVERROR(EINVAL);
4057 if (s->out_transpose)
4060 if (s->in_transpose)
4068 fov_from_dfov(s->out, s->d_fov, w, h, &s->h_fov, &s->v_fov);
4071 err = prepare_out(ctx);
4076 set_dimensions(s->pr_width, s->pr_height, w, h, desc);
4078 switch (s->out_stereo) {
4080 out_offset_w = out_offset_h = 0;
4096 set_dimensions(s->out_offset_w, s->out_offset_h, out_offset_w, out_offset_h, desc);
4097 set_dimensions(s->planewidth, s->planeheight, w, h, desc);
4099 for (int i = 0; i < 4; i++)
4100 s->uv_linesize[i] = FFALIGN(s->pr_width[i], 8);
4105 s->nb_planes = av_pix_fmt_count_planes(inlink->format);
4106 have_alpha = !!(desc->flags & AV_PIX_FMT_FLAG_ALPHA);
4108 if (desc->log2_chroma_h == desc->log2_chroma_w && desc->log2_chroma_h == 0) {
4109 s->nb_allocated = 1;
4110 s->map[0] = s->map[1] = s->map[2] = s->map[3] = 0;
4112 s->nb_allocated = 2;
4113 s->map[0] = s->map[3] = 0;
4114 s->map[1] = s->map[2] = 1;
4117 for (int i = 0; i < s->nb_allocated; i++)
4118 allocate_plane(s, sizeof_uv, sizeof_ker, sizeof_mask * have_alpha * s->alpha, i);
4120 calculate_rotation_matrix(s->yaw, s->pitch, s->roll, s->rot_mat, s->rotation_order);
4121 set_mirror_modifier(s->h_flip, s->v_flip, s->d_flip, s->output_mirror_modifier);
4123 ctx->internal->execute(ctx, v360_slice, NULL, NULL, FFMIN(outlink->h, ff_filter_get_nb_threads(ctx)));
4128 static int filter_frame(AVFilterLink *inlink, AVFrame *in)
4130 AVFilterContext *ctx = inlink->dst;
4131 AVFilterLink *outlink = ctx->outputs[0];
4132 V360Context *s = ctx->priv;
4136 out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
4139 return AVERROR(ENOMEM);
4141 av_frame_copy_props(out, in);
4146 ctx->internal->execute(ctx, s->remap_slice, &td, NULL, FFMIN(outlink->h, ff_filter_get_nb_threads(ctx)));
4149 return ff_filter_frame(outlink, out);
4152 static int process_command(AVFilterContext *ctx, const char *cmd, const char *args,
4153 char *res, int res_len, int flags)
4157 ret = ff_filter_process_command(ctx, cmd, args, res, res_len, flags);
4161 return config_output(ctx->outputs[0]);
4164 static av_cold void uninit(AVFilterContext *ctx)
4166 V360Context *s = ctx->priv;
4168 for (int p = 0; p < s->nb_allocated; p++) {
4171 av_freep(&s->ker[p]);
4176 static const AVFilterPad inputs[] = {
4179 .type = AVMEDIA_TYPE_VIDEO,
4180 .filter_frame = filter_frame,
4185 static const AVFilterPad outputs[] = {
4188 .type = AVMEDIA_TYPE_VIDEO,
4189 .config_props = config_output,
4194 AVFilter ff_vf_v360 = {
4196 .description = NULL_IF_CONFIG_SMALL("Convert 360 projection of video."),
4197 .priv_size = sizeof(V360Context),
4199 .query_formats = query_formats,
4202 .priv_class = &v360_class,
4203 .flags = AVFILTER_FLAG_SLICE_THREADS,
4204 .process_command = process_command,