2 * Copyright (c) 2019 Eugene Lyapustin
4 * This file is part of FFmpeg.
6 * FFmpeg is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * FFmpeg is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with FFmpeg; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
23 * 360 video conversion filter.
24 * Principle of operation:
26 * (for each pixel in output frame)
27 * 1) Calculate OpenGL-like coordinates (x, y, z) for pixel position (i, j)
28 * 2) Apply 360 operations (rotation, mirror) to (x, y, z)
29 * 3) Calculate pixel position (u, v) in input frame
30 * 4) Calculate interpolation window and weight for each pixel
33 * 5) Remap input frame to output frame using precalculated data
38 #include "libavutil/avassert.h"
39 #include "libavutil/imgutils.h"
40 #include "libavutil/pixdesc.h"
41 #include "libavutil/opt.h"
48 typedef struct ThreadData {
53 #define OFFSET(x) offsetof(V360Context, x)
54 #define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
56 static const AVOption v360_options[] = {
57 { "input", "set input projection", OFFSET(in), AV_OPT_TYPE_INT, {.i64=EQUIRECTANGULAR}, 0, NB_PROJECTIONS-1, FLAGS, "in" },
58 { "e", "equirectangular", 0, AV_OPT_TYPE_CONST, {.i64=EQUIRECTANGULAR}, 0, 0, FLAGS, "in" },
59 { "equirect", "equirectangular", 0, AV_OPT_TYPE_CONST, {.i64=EQUIRECTANGULAR}, 0, 0, FLAGS, "in" },
60 { "c3x2", "cubemap 3x2", 0, AV_OPT_TYPE_CONST, {.i64=CUBEMAP_3_2}, 0, 0, FLAGS, "in" },
61 { "c6x1", "cubemap 6x1", 0, AV_OPT_TYPE_CONST, {.i64=CUBEMAP_6_1}, 0, 0, FLAGS, "in" },
62 { "eac", "equi-angular cubemap", 0, AV_OPT_TYPE_CONST, {.i64=EQUIANGULAR}, 0, 0, FLAGS, "in" },
63 { "dfisheye", "dual fisheye", 0, AV_OPT_TYPE_CONST, {.i64=DUAL_FISHEYE}, 0, 0, FLAGS, "in" },
64 { "flat", "regular video", 0, AV_OPT_TYPE_CONST, {.i64=FLAT}, 0, 0, FLAGS, "in" },
65 {"rectilinear", "regular video", 0, AV_OPT_TYPE_CONST, {.i64=FLAT}, 0, 0, FLAGS, "in" },
66 { "gnomonic", "regular video", 0, AV_OPT_TYPE_CONST, {.i64=FLAT}, 0, 0, FLAGS, "in" },
67 { "barrel", "barrel facebook's 360 format", 0, AV_OPT_TYPE_CONST, {.i64=BARREL}, 0, 0, FLAGS, "in" },
68 { "fb", "barrel facebook's 360 format", 0, AV_OPT_TYPE_CONST, {.i64=BARREL}, 0, 0, FLAGS, "in" },
69 { "c1x6", "cubemap 1x6", 0, AV_OPT_TYPE_CONST, {.i64=CUBEMAP_1_6}, 0, 0, FLAGS, "in" },
70 { "sg", "stereographic", 0, AV_OPT_TYPE_CONST, {.i64=STEREOGRAPHIC}, 0, 0, FLAGS, "in" },
71 { "mercator", "mercator", 0, AV_OPT_TYPE_CONST, {.i64=MERCATOR}, 0, 0, FLAGS, "in" },
72 { "ball", "ball", 0, AV_OPT_TYPE_CONST, {.i64=BALL}, 0, 0, FLAGS, "in" },
73 { "hammer", "hammer", 0, AV_OPT_TYPE_CONST, {.i64=HAMMER}, 0, 0, FLAGS, "in" },
74 {"sinusoidal", "sinusoidal", 0, AV_OPT_TYPE_CONST, {.i64=SINUSOIDAL}, 0, 0, FLAGS, "in" },
75 { "fisheye", "fisheye", 0, AV_OPT_TYPE_CONST, {.i64=FISHEYE}, 0, 0, FLAGS, "in" },
76 {"cylindrical", "cylindrical", 0, AV_OPT_TYPE_CONST, {.i64=CYLINDRICAL}, 0, 0, FLAGS, "in" },
77 { "output", "set output projection", OFFSET(out), AV_OPT_TYPE_INT, {.i64=CUBEMAP_3_2}, 0, NB_PROJECTIONS-1, FLAGS, "out" },
78 { "e", "equirectangular", 0, AV_OPT_TYPE_CONST, {.i64=EQUIRECTANGULAR}, 0, 0, FLAGS, "out" },
79 { "equirect", "equirectangular", 0, AV_OPT_TYPE_CONST, {.i64=EQUIRECTANGULAR}, 0, 0, FLAGS, "out" },
80 { "c3x2", "cubemap 3x2", 0, AV_OPT_TYPE_CONST, {.i64=CUBEMAP_3_2}, 0, 0, FLAGS, "out" },
81 { "c6x1", "cubemap 6x1", 0, AV_OPT_TYPE_CONST, {.i64=CUBEMAP_6_1}, 0, 0, FLAGS, "out" },
82 { "eac", "equi-angular cubemap", 0, AV_OPT_TYPE_CONST, {.i64=EQUIANGULAR}, 0, 0, FLAGS, "out" },
83 { "dfisheye", "dual fisheye", 0, AV_OPT_TYPE_CONST, {.i64=DUAL_FISHEYE}, 0, 0, FLAGS, "out" },
84 { "flat", "regular video", 0, AV_OPT_TYPE_CONST, {.i64=FLAT}, 0, 0, FLAGS, "out" },
85 {"rectilinear", "regular video", 0, AV_OPT_TYPE_CONST, {.i64=FLAT}, 0, 0, FLAGS, "out" },
86 { "gnomonic", "regular video", 0, AV_OPT_TYPE_CONST, {.i64=FLAT}, 0, 0, FLAGS, "out" },
87 { "barrel", "barrel facebook's 360 format", 0, AV_OPT_TYPE_CONST, {.i64=BARREL}, 0, 0, FLAGS, "out" },
88 { "fb", "barrel facebook's 360 format", 0, AV_OPT_TYPE_CONST, {.i64=BARREL}, 0, 0, FLAGS, "out" },
89 { "c1x6", "cubemap 1x6", 0, AV_OPT_TYPE_CONST, {.i64=CUBEMAP_1_6}, 0, 0, FLAGS, "out" },
90 { "sg", "stereographic", 0, AV_OPT_TYPE_CONST, {.i64=STEREOGRAPHIC}, 0, 0, FLAGS, "out" },
91 { "mercator", "mercator", 0, AV_OPT_TYPE_CONST, {.i64=MERCATOR}, 0, 0, FLAGS, "out" },
92 { "ball", "ball", 0, AV_OPT_TYPE_CONST, {.i64=BALL}, 0, 0, FLAGS, "out" },
93 { "hammer", "hammer", 0, AV_OPT_TYPE_CONST, {.i64=HAMMER}, 0, 0, FLAGS, "out" },
94 {"sinusoidal", "sinusoidal", 0, AV_OPT_TYPE_CONST, {.i64=SINUSOIDAL}, 0, 0, FLAGS, "out" },
95 { "fisheye", "fisheye", 0, AV_OPT_TYPE_CONST, {.i64=FISHEYE}, 0, 0, FLAGS, "out" },
96 { "pannini", "pannini", 0, AV_OPT_TYPE_CONST, {.i64=PANNINI}, 0, 0, FLAGS, "out" },
97 {"cylindrical", "cylindrical", 0, AV_OPT_TYPE_CONST, {.i64=CYLINDRICAL}, 0, 0, FLAGS, "out" },
98 {"perspective", "perspective", 0, AV_OPT_TYPE_CONST, {.i64=PERSPECTIVE}, 0, 0, FLAGS, "out" },
99 { "interp", "set interpolation method", OFFSET(interp), AV_OPT_TYPE_INT, {.i64=BILINEAR}, 0, NB_INTERP_METHODS-1, FLAGS, "interp" },
100 { "near", "nearest neighbour", 0, AV_OPT_TYPE_CONST, {.i64=NEAREST}, 0, 0, FLAGS, "interp" },
101 { "nearest", "nearest neighbour", 0, AV_OPT_TYPE_CONST, {.i64=NEAREST}, 0, 0, FLAGS, "interp" },
102 { "line", "bilinear interpolation", 0, AV_OPT_TYPE_CONST, {.i64=BILINEAR}, 0, 0, FLAGS, "interp" },
103 { "linear", "bilinear interpolation", 0, AV_OPT_TYPE_CONST, {.i64=BILINEAR}, 0, 0, FLAGS, "interp" },
104 { "cube", "bicubic interpolation", 0, AV_OPT_TYPE_CONST, {.i64=BICUBIC}, 0, 0, FLAGS, "interp" },
105 { "cubic", "bicubic interpolation", 0, AV_OPT_TYPE_CONST, {.i64=BICUBIC}, 0, 0, FLAGS, "interp" },
106 { "lanc", "lanczos interpolation", 0, AV_OPT_TYPE_CONST, {.i64=LANCZOS}, 0, 0, FLAGS, "interp" },
107 { "lanczos", "lanczos interpolation", 0, AV_OPT_TYPE_CONST, {.i64=LANCZOS}, 0, 0, FLAGS, "interp" },
108 { "sp16", "spline16 interpolation", 0, AV_OPT_TYPE_CONST, {.i64=SPLINE16}, 0, 0, FLAGS, "interp" },
109 { "spline16", "spline16 interpolation", 0, AV_OPT_TYPE_CONST, {.i64=SPLINE16}, 0, 0, FLAGS, "interp" },
110 { "gauss", "gaussian interpolation", 0, AV_OPT_TYPE_CONST, {.i64=GAUSSIAN}, 0, 0, FLAGS, "interp" },
111 { "gaussian", "gaussian interpolation", 0, AV_OPT_TYPE_CONST, {.i64=GAUSSIAN}, 0, 0, FLAGS, "interp" },
112 { "w", "output width", OFFSET(width), AV_OPT_TYPE_INT, {.i64=0}, 0, INT16_MAX, FLAGS, "w"},
113 { "h", "output height", OFFSET(height), AV_OPT_TYPE_INT, {.i64=0}, 0, INT16_MAX, FLAGS, "h"},
114 { "in_stereo", "input stereo format", OFFSET(in_stereo), AV_OPT_TYPE_INT, {.i64=STEREO_2D}, 0, NB_STEREO_FMTS-1, FLAGS, "stereo" },
115 {"out_stereo", "output stereo format", OFFSET(out_stereo), AV_OPT_TYPE_INT, {.i64=STEREO_2D}, 0, NB_STEREO_FMTS-1, FLAGS, "stereo" },
116 { "2d", "2d mono", 0, AV_OPT_TYPE_CONST, {.i64=STEREO_2D}, 0, 0, FLAGS, "stereo" },
117 { "sbs", "side by side", 0, AV_OPT_TYPE_CONST, {.i64=STEREO_SBS}, 0, 0, FLAGS, "stereo" },
118 { "tb", "top bottom", 0, AV_OPT_TYPE_CONST, {.i64=STEREO_TB}, 0, 0, FLAGS, "stereo" },
119 { "in_forder", "input cubemap face order", OFFSET(in_forder), AV_OPT_TYPE_STRING, {.str="rludfb"}, 0, NB_DIRECTIONS-1, FLAGS, "in_forder"},
120 {"out_forder", "output cubemap face order", OFFSET(out_forder), AV_OPT_TYPE_STRING, {.str="rludfb"}, 0, NB_DIRECTIONS-1, FLAGS, "out_forder"},
121 { "in_frot", "input cubemap face rotation", OFFSET(in_frot), AV_OPT_TYPE_STRING, {.str="000000"}, 0, NB_DIRECTIONS-1, FLAGS, "in_frot"},
122 { "out_frot", "output cubemap face rotation",OFFSET(out_frot), AV_OPT_TYPE_STRING, {.str="000000"}, 0, NB_DIRECTIONS-1, FLAGS, "out_frot"},
123 { "in_pad", "percent input cubemap pads", OFFSET(in_pad), AV_OPT_TYPE_FLOAT, {.dbl=0.f}, 0.f, 1.f, FLAGS, "in_pad"},
124 { "out_pad", "percent output cubemap pads", OFFSET(out_pad), AV_OPT_TYPE_FLOAT, {.dbl=0.f}, 0.f, 1.f, FLAGS, "out_pad"},
125 { "fin_pad", "fixed input cubemap pads", OFFSET(fin_pad), AV_OPT_TYPE_INT, {.i64=0}, 0, 100, FLAGS, "fin_pad"},
126 { "fout_pad", "fixed output cubemap pads", OFFSET(fout_pad), AV_OPT_TYPE_INT, {.i64=0}, 0, 100, FLAGS, "fout_pad"},
127 { "yaw", "yaw rotation", OFFSET(yaw), AV_OPT_TYPE_FLOAT, {.dbl=0.f}, -180.f, 180.f, FLAGS, "yaw"},
128 { "pitch", "pitch rotation", OFFSET(pitch), AV_OPT_TYPE_FLOAT, {.dbl=0.f}, -180.f, 180.f, FLAGS, "pitch"},
129 { "roll", "roll rotation", OFFSET(roll), AV_OPT_TYPE_FLOAT, {.dbl=0.f}, -180.f, 180.f, FLAGS, "roll"},
130 { "rorder", "rotation order", OFFSET(rorder), AV_OPT_TYPE_STRING, {.str="ypr"}, 0, 0, FLAGS, "rorder"},
131 { "h_fov", "horizontal field of view", OFFSET(h_fov), AV_OPT_TYPE_FLOAT, {.dbl=90.f}, 0.00001f, 360.f, FLAGS, "h_fov"},
132 { "v_fov", "vertical field of view", OFFSET(v_fov), AV_OPT_TYPE_FLOAT, {.dbl=45.f}, 0.00001f, 360.f, FLAGS, "v_fov"},
133 { "d_fov", "diagonal field of view", OFFSET(d_fov), AV_OPT_TYPE_FLOAT, {.dbl=0.f}, 0.f, 360.f, FLAGS, "d_fov"},
134 { "h_flip", "flip out video horizontally", OFFSET(h_flip), AV_OPT_TYPE_BOOL, {.i64=0}, 0, 1, FLAGS, "h_flip"},
135 { "v_flip", "flip out video vertically", OFFSET(v_flip), AV_OPT_TYPE_BOOL, {.i64=0}, 0, 1, FLAGS, "v_flip"},
136 { "d_flip", "flip out video indepth", OFFSET(d_flip), AV_OPT_TYPE_BOOL, {.i64=0}, 0, 1, FLAGS, "d_flip"},
137 { "ih_flip", "flip in video horizontally", OFFSET(ih_flip), AV_OPT_TYPE_BOOL, {.i64=0}, 0, 1, FLAGS, "ih_flip"},
138 { "iv_flip", "flip in video vertically", OFFSET(iv_flip), AV_OPT_TYPE_BOOL, {.i64=0}, 0, 1, FLAGS, "iv_flip"},
139 { "in_trans", "transpose video input", OFFSET(in_transpose), AV_OPT_TYPE_BOOL, {.i64=0}, 0, 1, FLAGS, "in_transpose"},
140 { "out_trans", "transpose video output", OFFSET(out_transpose), AV_OPT_TYPE_BOOL, {.i64=0}, 0, 1, FLAGS, "out_transpose"},
141 { "ih_fov", "input horizontal field of view",OFFSET(ih_fov), AV_OPT_TYPE_FLOAT, {.dbl=90.f}, 0.00001f, 360.f, FLAGS, "ih_fov"},
142 { "iv_fov", "input vertical field of view", OFFSET(iv_fov), AV_OPT_TYPE_FLOAT, {.dbl=45.f}, 0.00001f, 360.f, FLAGS, "iv_fov"},
143 { "id_fov", "input diagonal field of view", OFFSET(id_fov), AV_OPT_TYPE_FLOAT, {.dbl=0.f}, 0.f, 360.f, FLAGS, "id_fov"},
147 AVFILTER_DEFINE_CLASS(v360);
149 static int query_formats(AVFilterContext *ctx)
151 static const enum AVPixelFormat pix_fmts[] = {
153 AV_PIX_FMT_YUVA444P, AV_PIX_FMT_YUVA444P9,
154 AV_PIX_FMT_YUVA444P10, AV_PIX_FMT_YUVA444P12,
155 AV_PIX_FMT_YUVA444P16,
158 AV_PIX_FMT_YUVA422P, AV_PIX_FMT_YUVA422P9,
159 AV_PIX_FMT_YUVA422P10, AV_PIX_FMT_YUVA422P12,
160 AV_PIX_FMT_YUVA422P16,
163 AV_PIX_FMT_YUVA420P, AV_PIX_FMT_YUVA420P9,
164 AV_PIX_FMT_YUVA420P10, AV_PIX_FMT_YUVA420P16,
167 AV_PIX_FMT_YUVJ444P, AV_PIX_FMT_YUVJ440P,
168 AV_PIX_FMT_YUVJ422P, AV_PIX_FMT_YUVJ420P,
172 AV_PIX_FMT_YUV444P, AV_PIX_FMT_YUV444P9,
173 AV_PIX_FMT_YUV444P10, AV_PIX_FMT_YUV444P12,
174 AV_PIX_FMT_YUV444P14, AV_PIX_FMT_YUV444P16,
177 AV_PIX_FMT_YUV440P, AV_PIX_FMT_YUV440P10,
178 AV_PIX_FMT_YUV440P12,
181 AV_PIX_FMT_YUV422P, AV_PIX_FMT_YUV422P9,
182 AV_PIX_FMT_YUV422P10, AV_PIX_FMT_YUV422P12,
183 AV_PIX_FMT_YUV422P14, AV_PIX_FMT_YUV422P16,
186 AV_PIX_FMT_YUV420P, AV_PIX_FMT_YUV420P9,
187 AV_PIX_FMT_YUV420P10, AV_PIX_FMT_YUV420P12,
188 AV_PIX_FMT_YUV420P14, AV_PIX_FMT_YUV420P16,
197 AV_PIX_FMT_GBRP, AV_PIX_FMT_GBRP9,
198 AV_PIX_FMT_GBRP10, AV_PIX_FMT_GBRP12,
199 AV_PIX_FMT_GBRP14, AV_PIX_FMT_GBRP16,
202 AV_PIX_FMT_GBRAP, AV_PIX_FMT_GBRAP10,
203 AV_PIX_FMT_GBRAP12, AV_PIX_FMT_GBRAP16,
206 AV_PIX_FMT_GRAY8, AV_PIX_FMT_GRAY9,
207 AV_PIX_FMT_GRAY10, AV_PIX_FMT_GRAY12,
208 AV_PIX_FMT_GRAY14, AV_PIX_FMT_GRAY16,
213 AVFilterFormats *fmts_list = ff_make_format_list(pix_fmts);
215 return AVERROR(ENOMEM);
216 return ff_set_common_formats(ctx, fmts_list);
219 #define DEFINE_REMAP1_LINE(bits, div) \
220 static void remap1_##bits##bit_line_c(uint8_t *dst, int width, const uint8_t *const src, \
221 ptrdiff_t in_linesize, \
222 const int16_t *const u, const int16_t *const v, \
223 const int16_t *const ker) \
225 const uint##bits##_t *const s = (const uint##bits##_t *const)src; \
226 uint##bits##_t *d = (uint##bits##_t *)dst; \
228 in_linesize /= div; \
230 for (int x = 0; x < width; x++) \
231 d[x] = s[v[x] * in_linesize + u[x]]; \
234 DEFINE_REMAP1_LINE( 8, 1)
235 DEFINE_REMAP1_LINE(16, 2)
238 * Generate remapping function with a given window size and pixel depth.
240 * @param ws size of interpolation window
241 * @param bits number of bits per pixel
243 #define DEFINE_REMAP(ws, bits) \
244 static int remap##ws##_##bits##bit_slice(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs) \
246 ThreadData *td = arg; \
247 const V360Context *s = ctx->priv; \
248 const AVFrame *in = td->in; \
249 AVFrame *out = td->out; \
251 for (int stereo = 0; stereo < 1 + s->out_stereo > STEREO_2D; stereo++) { \
252 for (int plane = 0; plane < s->nb_planes; plane++) { \
253 const unsigned map = s->map[plane]; \
254 const int in_linesize = in->linesize[plane]; \
255 const int out_linesize = out->linesize[plane]; \
256 const int uv_linesize = s->uv_linesize[plane]; \
257 const int in_offset_w = stereo ? s->in_offset_w[plane] : 0; \
258 const int in_offset_h = stereo ? s->in_offset_h[plane] : 0; \
259 const int out_offset_w = stereo ? s->out_offset_w[plane] : 0; \
260 const int out_offset_h = stereo ? s->out_offset_h[plane] : 0; \
261 const uint8_t *const src = in->data[plane] + \
262 in_offset_h * in_linesize + in_offset_w * (bits >> 3); \
263 uint8_t *dst = out->data[plane] + out_offset_h * out_linesize + out_offset_w * (bits >> 3); \
264 const int width = s->pr_width[plane]; \
265 const int height = s->pr_height[plane]; \
267 const int slice_start = (height * jobnr ) / nb_jobs; \
268 const int slice_end = (height * (jobnr + 1)) / nb_jobs; \
270 for (int y = slice_start; y < slice_end; y++) { \
271 const int16_t *const u = s->u[map] + y * uv_linesize * ws * ws; \
272 const int16_t *const v = s->v[map] + y * uv_linesize * ws * ws; \
273 const int16_t *const ker = s->ker[map] + y * uv_linesize * ws * ws; \
275 s->remap_line(dst + y * out_linesize, width, src, in_linesize, u, v, ker); \
290 #define DEFINE_REMAP_LINE(ws, bits, div) \
291 static void remap##ws##_##bits##bit_line_c(uint8_t *dst, int width, const uint8_t *const src, \
292 ptrdiff_t in_linesize, \
293 const int16_t *const u, const int16_t *const v, \
294 const int16_t *const ker) \
296 const uint##bits##_t *const s = (const uint##bits##_t *const)src; \
297 uint##bits##_t *d = (uint##bits##_t *)dst; \
299 in_linesize /= div; \
301 for (int x = 0; x < width; x++) { \
302 const int16_t *const uu = u + x * ws * ws; \
303 const int16_t *const vv = v + x * ws * ws; \
304 const int16_t *const kker = ker + x * ws * ws; \
307 for (int i = 0; i < ws; i++) { \
308 for (int j = 0; j < ws; j++) { \
309 tmp += kker[i * ws + j] * s[vv[i * ws + j] * in_linesize + uu[i * ws + j]]; \
313 d[x] = av_clip_uint##bits(tmp >> 14); \
317 DEFINE_REMAP_LINE(2, 8, 1)
318 DEFINE_REMAP_LINE(4, 8, 1)
319 DEFINE_REMAP_LINE(2, 16, 2)
320 DEFINE_REMAP_LINE(4, 16, 2)
322 void ff_v360_init(V360Context *s, int depth)
326 s->remap_line = depth <= 8 ? remap1_8bit_line_c : remap1_16bit_line_c;
329 s->remap_line = depth <= 8 ? remap2_8bit_line_c : remap2_16bit_line_c;
335 s->remap_line = depth <= 8 ? remap4_8bit_line_c : remap4_16bit_line_c;
340 ff_v360_init_x86(s, depth);
344 * Save nearest pixel coordinates for remapping.
346 * @param du horizontal relative coordinate
347 * @param dv vertical relative coordinate
348 * @param rmap calculated 4x4 window
349 * @param u u remap data
350 * @param v v remap data
351 * @param ker ker remap data
353 static void nearest_kernel(float du, float dv, const XYRemap *rmap,
354 int16_t *u, int16_t *v, int16_t *ker)
356 const int i = lrintf(dv) + 1;
357 const int j = lrintf(du) + 1;
359 u[0] = rmap->u[i][j];
360 v[0] = rmap->v[i][j];
364 * Calculate kernel for bilinear interpolation.
366 * @param du horizontal relative coordinate
367 * @param dv vertical relative coordinate
368 * @param rmap calculated 4x4 window
369 * @param u u remap data
370 * @param v v remap data
371 * @param ker ker remap data
373 static void bilinear_kernel(float du, float dv, const XYRemap *rmap,
374 int16_t *u, int16_t *v, int16_t *ker)
376 for (int i = 0; i < 2; i++) {
377 for (int j = 0; j < 2; j++) {
378 u[i * 2 + j] = rmap->u[i + 1][j + 1];
379 v[i * 2 + j] = rmap->v[i + 1][j + 1];
383 ker[0] = lrintf((1.f - du) * (1.f - dv) * 16385.f);
384 ker[1] = lrintf( du * (1.f - dv) * 16385.f);
385 ker[2] = lrintf((1.f - du) * dv * 16385.f);
386 ker[3] = lrintf( du * dv * 16385.f);
390 * Calculate 1-dimensional cubic coefficients.
392 * @param t relative coordinate
393 * @param coeffs coefficients
395 static inline void calculate_bicubic_coeffs(float t, float *coeffs)
397 const float tt = t * t;
398 const float ttt = t * t * t;
400 coeffs[0] = - t / 3.f + tt / 2.f - ttt / 6.f;
401 coeffs[1] = 1.f - t / 2.f - tt + ttt / 2.f;
402 coeffs[2] = t + tt / 2.f - ttt / 2.f;
403 coeffs[3] = - t / 6.f + ttt / 6.f;
407 * Calculate kernel for bicubic interpolation.
409 * @param du horizontal relative coordinate
410 * @param dv vertical relative coordinate
411 * @param rmap calculated 4x4 window
412 * @param u u remap data
413 * @param v v remap data
414 * @param ker ker remap data
416 static void bicubic_kernel(float du, float dv, const XYRemap *rmap,
417 int16_t *u, int16_t *v, int16_t *ker)
422 calculate_bicubic_coeffs(du, du_coeffs);
423 calculate_bicubic_coeffs(dv, dv_coeffs);
425 for (int i = 0; i < 4; i++) {
426 for (int j = 0; j < 4; j++) {
427 u[i * 4 + j] = rmap->u[i][j];
428 v[i * 4 + j] = rmap->v[i][j];
429 ker[i * 4 + j] = lrintf(du_coeffs[j] * dv_coeffs[i] * 16385.f);
435 * Calculate 1-dimensional lanczos coefficients.
437 * @param t relative coordinate
438 * @param coeffs coefficients
440 static inline void calculate_lanczos_coeffs(float t, float *coeffs)
444 for (int i = 0; i < 4; i++) {
445 const float x = M_PI * (t - i + 1);
449 coeffs[i] = sinf(x) * sinf(x / 2.f) / (x * x / 2.f);
454 for (int i = 0; i < 4; i++) {
460 * Calculate kernel for lanczos interpolation.
462 * @param du horizontal relative coordinate
463 * @param dv vertical relative coordinate
464 * @param rmap calculated 4x4 window
465 * @param u u remap data
466 * @param v v remap data
467 * @param ker ker remap data
469 static void lanczos_kernel(float du, float dv, const XYRemap *rmap,
470 int16_t *u, int16_t *v, int16_t *ker)
475 calculate_lanczos_coeffs(du, du_coeffs);
476 calculate_lanczos_coeffs(dv, dv_coeffs);
478 for (int i = 0; i < 4; i++) {
479 for (int j = 0; j < 4; j++) {
480 u[i * 4 + j] = rmap->u[i][j];
481 v[i * 4 + j] = rmap->v[i][j];
482 ker[i * 4 + j] = lrintf(du_coeffs[j] * dv_coeffs[i] * 16385.f);
488 * Calculate 1-dimensional spline16 coefficients.
490 * @param t relative coordinate
491 * @param coeffs coefficients
493 static void calculate_spline16_coeffs(float t, float *coeffs)
495 coeffs[0] = ((-1.f / 3.f * t + 0.8f) * t - 7.f / 15.f) * t;
496 coeffs[1] = ((t - 9.f / 5.f) * t - 0.2f) * t + 1.f;
497 coeffs[2] = ((6.f / 5.f - t) * t + 0.8f) * t;
498 coeffs[3] = ((1.f / 3.f * t - 0.2f) * t - 2.f / 15.f) * t;
502 * Calculate kernel for spline16 interpolation.
504 * @param du horizontal relative coordinate
505 * @param dv vertical relative coordinate
506 * @param rmap calculated 4x4 window
507 * @param u u remap data
508 * @param v v remap data
509 * @param ker ker remap data
511 static void spline16_kernel(float du, float dv, const XYRemap *rmap,
512 int16_t *u, int16_t *v, int16_t *ker)
517 calculate_spline16_coeffs(du, du_coeffs);
518 calculate_spline16_coeffs(dv, dv_coeffs);
520 for (int i = 0; i < 4; i++) {
521 for (int j = 0; j < 4; j++) {
522 u[i * 4 + j] = rmap->u[i][j];
523 v[i * 4 + j] = rmap->v[i][j];
524 ker[i * 4 + j] = lrintf(du_coeffs[j] * dv_coeffs[i] * 16385.f);
530 * Calculate 1-dimensional gaussian coefficients.
532 * @param t relative coordinate
533 * @param coeffs coefficients
535 static void calculate_gaussian_coeffs(float t, float *coeffs)
539 for (int i = 0; i < 4; i++) {
540 const float x = t - (i - 1);
544 coeffs[i] = expf(-2.f * x * x) * expf(-x * x / 2.f);
549 for (int i = 0; i < 4; i++) {
555 * Calculate kernel for gaussian interpolation.
557 * @param du horizontal relative coordinate
558 * @param dv vertical relative coordinate
559 * @param rmap calculated 4x4 window
560 * @param u u remap data
561 * @param v v remap data
562 * @param ker ker remap data
564 static void gaussian_kernel(float du, float dv, const XYRemap *rmap,
565 int16_t *u, int16_t *v, int16_t *ker)
570 calculate_gaussian_coeffs(du, du_coeffs);
571 calculate_gaussian_coeffs(dv, dv_coeffs);
573 for (int i = 0; i < 4; i++) {
574 for (int j = 0; j < 4; j++) {
575 u[i * 4 + j] = rmap->u[i][j];
576 v[i * 4 + j] = rmap->v[i][j];
577 ker[i * 4 + j] = lrintf(du_coeffs[j] * dv_coeffs[i] * 16385.f);
583 * Modulo operation with only positive remainders.
588 * @return positive remainder of (a / b)
590 static inline int mod(int a, int b)
592 const int res = a % b;
601 * Convert char to corresponding direction.
602 * Used for cubemap options.
604 static int get_direction(char c)
625 * Convert char to corresponding rotation angle.
626 * Used for cubemap options.
628 static int get_rotation(char c)
645 * Convert char to corresponding rotation order.
647 static int get_rorder(char c)
665 * Prepare data for processing cubemap input format.
667 * @param ctx filter context
671 static int prepare_cube_in(AVFilterContext *ctx)
673 V360Context *s = ctx->priv;
675 for (int face = 0; face < NB_FACES; face++) {
676 const char c = s->in_forder[face];
680 av_log(ctx, AV_LOG_ERROR,
681 "Incomplete in_forder option. Direction for all 6 faces should be specified.\n");
682 return AVERROR(EINVAL);
685 direction = get_direction(c);
686 if (direction == -1) {
687 av_log(ctx, AV_LOG_ERROR,
688 "Incorrect direction symbol '%c' in in_forder option.\n", c);
689 return AVERROR(EINVAL);
692 s->in_cubemap_face_order[direction] = face;
695 for (int face = 0; face < NB_FACES; face++) {
696 const char c = s->in_frot[face];
700 av_log(ctx, AV_LOG_ERROR,
701 "Incomplete in_frot option. Rotation for all 6 faces should be specified.\n");
702 return AVERROR(EINVAL);
705 rotation = get_rotation(c);
706 if (rotation == -1) {
707 av_log(ctx, AV_LOG_ERROR,
708 "Incorrect rotation symbol '%c' in in_frot option.\n", c);
709 return AVERROR(EINVAL);
712 s->in_cubemap_face_rotation[face] = rotation;
719 * Prepare data for processing cubemap output format.
721 * @param ctx filter context
725 static int prepare_cube_out(AVFilterContext *ctx)
727 V360Context *s = ctx->priv;
729 for (int face = 0; face < NB_FACES; face++) {
730 const char c = s->out_forder[face];
734 av_log(ctx, AV_LOG_ERROR,
735 "Incomplete out_forder option. Direction for all 6 faces should be specified.\n");
736 return AVERROR(EINVAL);
739 direction = get_direction(c);
740 if (direction == -1) {
741 av_log(ctx, AV_LOG_ERROR,
742 "Incorrect direction symbol '%c' in out_forder option.\n", c);
743 return AVERROR(EINVAL);
746 s->out_cubemap_direction_order[face] = direction;
749 for (int face = 0; face < NB_FACES; face++) {
750 const char c = s->out_frot[face];
754 av_log(ctx, AV_LOG_ERROR,
755 "Incomplete out_frot option. Rotation for all 6 faces should be specified.\n");
756 return AVERROR(EINVAL);
759 rotation = get_rotation(c);
760 if (rotation == -1) {
761 av_log(ctx, AV_LOG_ERROR,
762 "Incorrect rotation symbol '%c' in out_frot option.\n", c);
763 return AVERROR(EINVAL);
766 s->out_cubemap_face_rotation[face] = rotation;
772 static inline void rotate_cube_face(float *uf, float *vf, int rotation)
798 static inline void rotate_cube_face_inverse(float *uf, float *vf, int rotation)
829 static void normalize_vector(float *vec)
831 const float norm = sqrtf(vec[0] * vec[0] + vec[1] * vec[1] + vec[2] * vec[2]);
839 * Calculate 3D coordinates on sphere for corresponding cubemap position.
840 * Common operation for every cubemap.
842 * @param s filter private context
843 * @param uf horizontal cubemap coordinate [0, 1)
844 * @param vf vertical cubemap coordinate [0, 1)
845 * @param face face of cubemap
846 * @param vec coordinates on sphere
847 * @param scalew scale for uf
848 * @param scaleh scale for vf
850 static void cube_to_xyz(const V360Context *s,
851 float uf, float vf, int face,
852 float *vec, float scalew, float scaleh)
854 const int direction = s->out_cubemap_direction_order[face];
860 rotate_cube_face_inverse(&uf, &vf, s->out_cubemap_face_rotation[face]);
901 normalize_vector(vec);
905 * Calculate cubemap position for corresponding 3D coordinates on sphere.
906 * Common operation for every cubemap.
908 * @param s filter private context
909 * @param vec coordinated on sphere
910 * @param uf horizontal cubemap coordinate [0, 1)
911 * @param vf vertical cubemap coordinate [0, 1)
912 * @param direction direction of view
914 static void xyz_to_cube(const V360Context *s,
916 float *uf, float *vf, int *direction)
918 const float phi = atan2f(vec[0], -vec[2]);
919 const float theta = asinf(-vec[1]);
920 float phi_norm, theta_threshold;
923 if (phi >= -M_PI_4 && phi < M_PI_4) {
926 } else if (phi >= -(M_PI_2 + M_PI_4) && phi < -M_PI_4) {
928 phi_norm = phi + M_PI_2;
929 } else if (phi >= M_PI_4 && phi < M_PI_2 + M_PI_4) {
931 phi_norm = phi - M_PI_2;
934 phi_norm = phi + ((phi > 0.f) ? -M_PI : M_PI);
937 theta_threshold = atanf(cosf(phi_norm));
938 if (theta > theta_threshold) {
940 } else if (theta < -theta_threshold) {
944 switch (*direction) {
946 *uf = vec[2] / vec[0];
947 *vf = -vec[1] / vec[0];
950 *uf = vec[2] / vec[0];
951 *vf = vec[1] / vec[0];
954 *uf = vec[0] / vec[1];
955 *vf = -vec[2] / vec[1];
958 *uf = -vec[0] / vec[1];
959 *vf = -vec[2] / vec[1];
962 *uf = -vec[0] / vec[2];
963 *vf = vec[1] / vec[2];
966 *uf = -vec[0] / vec[2];
967 *vf = -vec[1] / vec[2];
973 face = s->in_cubemap_face_order[*direction];
974 rotate_cube_face(uf, vf, s->in_cubemap_face_rotation[face]);
976 (*uf) *= s->input_mirror_modifier[0];
977 (*vf) *= s->input_mirror_modifier[1];
981 * Find position on another cube face in case of overflow/underflow.
982 * Used for calculation of interpolation window.
984 * @param s filter private context
985 * @param uf horizontal cubemap coordinate
986 * @param vf vertical cubemap coordinate
987 * @param direction direction of view
988 * @param new_uf new horizontal cubemap coordinate
989 * @param new_vf new vertical cubemap coordinate
990 * @param face face position on cubemap
992 static void process_cube_coordinates(const V360Context *s,
993 float uf, float vf, int direction,
994 float *new_uf, float *new_vf, int *face)
997 * Cubemap orientation
1004 * +-------+-------+-------+-------+ ^ e |
1006 * | left | front | right | back | | g |
1007 * +-------+-------+-------+-------+ v h v
1013 *face = s->in_cubemap_face_order[direction];
1014 rotate_cube_face_inverse(&uf, &vf, s->in_cubemap_face_rotation[*face]);
1016 if ((uf < -1.f || uf >= 1.f) && (vf < -1.f || vf >= 1.f)) {
1017 // There are no pixels to use in this case
1020 } else if (uf < -1.f) {
1022 switch (direction) {
1056 } else if (uf >= 1.f) {
1058 switch (direction) {
1092 } else if (vf < -1.f) {
1094 switch (direction) {
1128 } else if (vf >= 1.f) {
1130 switch (direction) {
1170 *face = s->in_cubemap_face_order[direction];
1171 rotate_cube_face(new_uf, new_vf, s->in_cubemap_face_rotation[*face]);
1175 * Calculate 3D coordinates on sphere for corresponding frame position in cubemap3x2 format.
1177 * @param s filter private context
1178 * @param i horizontal position on frame [0, width)
1179 * @param j vertical position on frame [0, height)
1180 * @param width frame width
1181 * @param height frame height
1182 * @param vec coordinates on sphere
1184 static void cube3x2_to_xyz(const V360Context *s,
1185 int i, int j, int width, int height,
1188 const float scalew = s->fout_pad > 0 ? 1.f - s->fout_pad / (s->out_width / 3.f) : 1.f - s->out_pad;
1189 const float scaleh = s->fout_pad > 0 ? 1.f - s->fout_pad / (s->out_height / 2.f) : 1.f - s->out_pad;
1191 const float ew = width / 3.f;
1192 const float eh = height / 2.f;
1194 const int u_face = floorf(i / ew);
1195 const int v_face = floorf(j / eh);
1196 const int face = u_face + 3 * v_face;
1198 const int u_shift = ceilf(ew * u_face);
1199 const int v_shift = ceilf(eh * v_face);
1200 const int ewi = ceilf(ew * (u_face + 1)) - u_shift;
1201 const int ehi = ceilf(eh * (v_face + 1)) - v_shift;
1203 const float uf = 2.f * (i - u_shift + 0.5f) / ewi - 1.f;
1204 const float vf = 2.f * (j - v_shift + 0.5f) / ehi - 1.f;
1206 cube_to_xyz(s, uf, vf, face, vec, scalew, scaleh);
1210 * Calculate frame position in cubemap3x2 format for corresponding 3D coordinates on sphere.
1212 * @param s filter private context
1213 * @param vec coordinates on sphere
1214 * @param width frame width
1215 * @param height frame height
1216 * @param us horizontal coordinates for interpolation window
1217 * @param vs vertical coordinates for interpolation window
1218 * @param du horizontal relative coordinate
1219 * @param dv vertical relative coordinate
1221 static void xyz_to_cube3x2(const V360Context *s,
1222 const float *vec, int width, int height,
1223 int16_t us[4][4], int16_t vs[4][4], float *du, float *dv)
1225 const float scalew = s->fin_pad > 0 ? 1.f - s->fin_pad / (s->in_width / 3.f) : 1.f - s->in_pad;
1226 const float scaleh = s->fin_pad > 0 ? 1.f - s->fin_pad / (s->in_height / 2.f) : 1.f - s->in_pad;
1227 const float ew = width / 3.f;
1228 const float eh = height / 2.f;
1232 int direction, face;
1235 xyz_to_cube(s, vec, &uf, &vf, &direction);
1240 face = s->in_cubemap_face_order[direction];
1243 ewi = ceilf(ew * (u_face + 1)) - ceilf(ew * u_face);
1244 ehi = ceilf(eh * (v_face + 1)) - ceilf(eh * v_face);
1246 uf = 0.5f * ewi * (uf + 1.f) - 0.5f;
1247 vf = 0.5f * ehi * (vf + 1.f) - 0.5f;
1255 for (int i = -1; i < 3; i++) {
1256 for (int j = -1; j < 3; j++) {
1257 int new_ui = ui + j;
1258 int new_vi = vi + i;
1259 int u_shift, v_shift;
1260 int new_ewi, new_ehi;
1262 if (new_ui >= 0 && new_ui < ewi && new_vi >= 0 && new_vi < ehi) {
1263 face = s->in_cubemap_face_order[direction];
1267 u_shift = ceilf(ew * u_face);
1268 v_shift = ceilf(eh * v_face);
1270 uf = 2.f * new_ui / ewi - 1.f;
1271 vf = 2.f * new_vi / ehi - 1.f;
1276 process_cube_coordinates(s, uf, vf, direction, &uf, &vf, &face);
1283 u_shift = ceilf(ew * u_face);
1284 v_shift = ceilf(eh * v_face);
1285 new_ewi = ceilf(ew * (u_face + 1)) - u_shift;
1286 new_ehi = ceilf(eh * (v_face + 1)) - v_shift;
1288 new_ui = av_clip(lrintf(0.5f * new_ewi * (uf + 1.f)), 0, new_ewi - 1);
1289 new_vi = av_clip(lrintf(0.5f * new_ehi * (vf + 1.f)), 0, new_ehi - 1);
1292 us[i + 1][j + 1] = u_shift + new_ui;
1293 vs[i + 1][j + 1] = v_shift + new_vi;
1299 * Calculate 3D coordinates on sphere for corresponding frame position in cubemap1x6 format.
1301 * @param s filter private context
1302 * @param i horizontal position on frame [0, width)
1303 * @param j vertical position on frame [0, height)
1304 * @param width frame width
1305 * @param height frame height
1306 * @param vec coordinates on sphere
1308 static void cube1x6_to_xyz(const V360Context *s,
1309 int i, int j, int width, int height,
1312 const float scalew = s->fout_pad > 0 ? 1.f - (float)(s->fout_pad) / s->out_width : 1.f - s->out_pad;
1313 const float scaleh = s->fout_pad > 0 ? 1.f - s->fout_pad / (s->out_height / 6.f) : 1.f - s->out_pad;
1315 const float ew = width;
1316 const float eh = height / 6.f;
1318 const int face = floorf(j / eh);
1320 const int v_shift = ceilf(eh * face);
1321 const int ehi = ceilf(eh * (face + 1)) - v_shift;
1323 const float uf = 2.f * (i + 0.5f) / ew - 1.f;
1324 const float vf = 2.f * (j - v_shift + 0.5f) / ehi - 1.f;
1326 cube_to_xyz(s, uf, vf, face, vec, scalew, scaleh);
1330 * Calculate 3D coordinates on sphere for corresponding frame position in cubemap6x1 format.
1332 * @param s filter private context
1333 * @param i horizontal position on frame [0, width)
1334 * @param j vertical position on frame [0, height)
1335 * @param width frame width
1336 * @param height frame height
1337 * @param vec coordinates on sphere
1339 static void cube6x1_to_xyz(const V360Context *s,
1340 int i, int j, int width, int height,
1343 const float scalew = s->fout_pad > 0 ? 1.f - s->fout_pad / (s->out_width / 6.f) : 1.f - s->out_pad;
1344 const float scaleh = s->fout_pad > 0 ? 1.f - (float)(s->fout_pad) / s->out_height : 1.f - s->out_pad;
1346 const float ew = width / 6.f;
1347 const float eh = height;
1349 const int face = floorf(i / ew);
1351 const int u_shift = ceilf(ew * face);
1352 const int ewi = ceilf(ew * (face + 1)) - u_shift;
1354 const float uf = 2.f * (i - u_shift + 0.5f) / ewi - 1.f;
1355 const float vf = 2.f * (j + 0.5f) / eh - 1.f;
1357 cube_to_xyz(s, uf, vf, face, vec, scalew, scaleh);
1361 * Calculate frame position in cubemap1x6 format for corresponding 3D coordinates on sphere.
1363 * @param s filter private context
1364 * @param vec coordinates on sphere
1365 * @param width frame width
1366 * @param height frame height
1367 * @param us horizontal coordinates for interpolation window
1368 * @param vs vertical coordinates for interpolation window
1369 * @param du horizontal relative coordinate
1370 * @param dv vertical relative coordinate
1372 static void xyz_to_cube1x6(const V360Context *s,
1373 const float *vec, int width, int height,
1374 int16_t us[4][4], int16_t vs[4][4], float *du, float *dv)
1376 const float scalew = s->fin_pad > 0 ? 1.f - (float)(s->fin_pad) / s->in_width : 1.f - s->in_pad;
1377 const float scaleh = s->fin_pad > 0 ? 1.f - s->fin_pad / (s->in_height / 6.f) : 1.f - s->in_pad;
1378 const float eh = height / 6.f;
1379 const int ewi = width;
1383 int direction, face;
1385 xyz_to_cube(s, vec, &uf, &vf, &direction);
1390 face = s->in_cubemap_face_order[direction];
1391 ehi = ceilf(eh * (face + 1)) - ceilf(eh * face);
1393 uf = 0.5f * ewi * (uf + 1.f) - 0.5f;
1394 vf = 0.5f * ehi * (vf + 1.f) - 0.5f;
1402 for (int i = -1; i < 3; i++) {
1403 for (int j = -1; j < 3; j++) {
1404 int new_ui = ui + j;
1405 int new_vi = vi + i;
1409 if (new_ui >= 0 && new_ui < ewi && new_vi >= 0 && new_vi < ehi) {
1410 face = s->in_cubemap_face_order[direction];
1412 v_shift = ceilf(eh * face);
1414 uf = 2.f * new_ui / ewi - 1.f;
1415 vf = 2.f * new_vi / ehi - 1.f;
1420 process_cube_coordinates(s, uf, vf, direction, &uf, &vf, &face);
1425 v_shift = ceilf(eh * face);
1426 new_ehi = ceilf(eh * (face + 1)) - v_shift;
1428 new_ui = av_clip(lrintf(0.5f * ewi * (uf + 1.f)), 0, ewi - 1);
1429 new_vi = av_clip(lrintf(0.5f * new_ehi * (vf + 1.f)), 0, new_ehi - 1);
1432 us[i + 1][j + 1] = new_ui;
1433 vs[i + 1][j + 1] = v_shift + new_vi;
1439 * Calculate frame position in cubemap6x1 format for corresponding 3D coordinates on sphere.
1441 * @param s filter private context
1442 * @param vec coordinates on sphere
1443 * @param width frame width
1444 * @param height frame height
1445 * @param us horizontal coordinates for interpolation window
1446 * @param vs vertical coordinates for interpolation window
1447 * @param du horizontal relative coordinate
1448 * @param dv vertical relative coordinate
1450 static void xyz_to_cube6x1(const V360Context *s,
1451 const float *vec, int width, int height,
1452 int16_t us[4][4], int16_t vs[4][4], float *du, float *dv)
1454 const float scalew = s->fin_pad > 0 ? 1.f - s->fin_pad / (s->in_width / 6.f) : 1.f - s->in_pad;
1455 const float scaleh = s->fin_pad > 0 ? 1.f - (float)(s->fin_pad) / s->in_height : 1.f - s->in_pad;
1456 const float ew = width / 6.f;
1457 const int ehi = height;
1461 int direction, face;
1463 xyz_to_cube(s, vec, &uf, &vf, &direction);
1468 face = s->in_cubemap_face_order[direction];
1469 ewi = ceilf(ew * (face + 1)) - ceilf(ew * face);
1471 uf = 0.5f * ewi * (uf + 1.f) - 0.5f;
1472 vf = 0.5f * ehi * (vf + 1.f) - 0.5f;
1480 for (int i = -1; i < 3; i++) {
1481 for (int j = -1; j < 3; j++) {
1482 int new_ui = ui + j;
1483 int new_vi = vi + i;
1487 if (new_ui >= 0 && new_ui < ewi && new_vi >= 0 && new_vi < ehi) {
1488 face = s->in_cubemap_face_order[direction];
1490 u_shift = ceilf(ew * face);
1492 uf = 2.f * new_ui / ewi - 1.f;
1493 vf = 2.f * new_vi / ehi - 1.f;
1498 process_cube_coordinates(s, uf, vf, direction, &uf, &vf, &face);
1503 u_shift = ceilf(ew * face);
1504 new_ewi = ceilf(ew * (face + 1)) - u_shift;
1506 new_ui = av_clip(lrintf(0.5f * new_ewi * (uf + 1.f)), 0, new_ewi - 1);
1507 new_vi = av_clip(lrintf(0.5f * ehi * (vf + 1.f)), 0, ehi - 1);
1510 us[i + 1][j + 1] = u_shift + new_ui;
1511 vs[i + 1][j + 1] = new_vi;
1517 * Calculate 3D coordinates on sphere for corresponding frame position in equirectangular format.
1519 * @param s filter private context
1520 * @param i horizontal position on frame [0, width)
1521 * @param j vertical position on frame [0, height)
1522 * @param width frame width
1523 * @param height frame height
1524 * @param vec coordinates on sphere
1526 static void equirect_to_xyz(const V360Context *s,
1527 int i, int j, int width, int height,
1530 const float phi = ((2.f * i) / width - 1.f) * M_PI;
1531 const float theta = ((2.f * j) / height - 1.f) * M_PI_2;
1533 const float sin_phi = sinf(phi);
1534 const float cos_phi = cosf(phi);
1535 const float sin_theta = sinf(theta);
1536 const float cos_theta = cosf(theta);
1538 vec[0] = cos_theta * sin_phi;
1539 vec[1] = -sin_theta;
1540 vec[2] = -cos_theta * cos_phi;
1544 * Prepare data for processing stereographic output format.
1546 * @param ctx filter context
1548 * @return error code
1550 static int prepare_stereographic_out(AVFilterContext *ctx)
1552 V360Context *s = ctx->priv;
1554 s->flat_range[0] = tanf(FFMIN(s->h_fov, 359.f) * M_PI / 720.f);
1555 s->flat_range[1] = tanf(FFMIN(s->v_fov, 359.f) * M_PI / 720.f);
1561 * Calculate 3D coordinates on sphere for corresponding frame position in stereographic format.
1563 * @param s filter private context
1564 * @param i horizontal position on frame [0, width)
1565 * @param j vertical position on frame [0, height)
1566 * @param width frame width
1567 * @param height frame height
1568 * @param vec coordinates on sphere
1570 static void stereographic_to_xyz(const V360Context *s,
1571 int i, int j, int width, int height,
1574 const float x = ((2.f * i) / width - 1.f) * s->flat_range[0];
1575 const float y = ((2.f * j) / height - 1.f) * s->flat_range[1];
1576 const float xy = x * x + y * y;
1578 vec[0] = 2.f * x / (1.f + xy);
1579 vec[1] = (-1.f + xy) / (1.f + xy);
1580 vec[2] = 2.f * y / (1.f + xy);
1582 normalize_vector(vec);
1586 * Prepare data for processing stereographic input format.
1588 * @param ctx filter context
1590 * @return error code
1592 static int prepare_stereographic_in(AVFilterContext *ctx)
1594 V360Context *s = ctx->priv;
1596 s->iflat_range[0] = tanf(FFMIN(s->ih_fov, 359.f) * M_PI / 720.f);
1597 s->iflat_range[1] = tanf(FFMIN(s->iv_fov, 359.f) * M_PI / 720.f);
1603 * Calculate frame position in stereographic format for corresponding 3D coordinates on sphere.
1605 * @param s filter private context
1606 * @param vec coordinates on sphere
1607 * @param width frame width
1608 * @param height frame height
1609 * @param us horizontal coordinates for interpolation window
1610 * @param vs vertical coordinates for interpolation window
1611 * @param du horizontal relative coordinate
1612 * @param dv vertical relative coordinate
1614 static void xyz_to_stereographic(const V360Context *s,
1615 const float *vec, int width, int height,
1616 int16_t us[4][4], int16_t vs[4][4], float *du, float *dv)
1618 const float x = vec[0] / (1.f - vec[1]) / s->iflat_range[0] * s->input_mirror_modifier[0];
1619 const float y = vec[2] / (1.f - vec[1]) / s->iflat_range[1] * s->input_mirror_modifier[1];
1621 int visible, ui, vi;
1623 uf = (x + 1.f) * width / 2.f;
1624 vf = (y + 1.f) * height / 2.f;
1628 visible = isfinite(x) && isfinite(y) && vi >= 0 && vi < height && ui >= 0 && ui < width;
1630 *du = visible ? uf - ui : 0.f;
1631 *dv = visible ? vf - vi : 0.f;
1633 for (int i = -1; i < 3; i++) {
1634 for (int j = -1; j < 3; j++) {
1635 us[i + 1][j + 1] = visible ? av_clip(ui + j, 0, width - 1) : 0;
1636 vs[i + 1][j + 1] = visible ? av_clip(vi + i, 0, height - 1) : 0;
1642 * Calculate frame position in equirectangular format for corresponding 3D coordinates on sphere.
1644 * @param s filter private context
1645 * @param vec coordinates on sphere
1646 * @param width frame width
1647 * @param height frame height
1648 * @param us horizontal coordinates for interpolation window
1649 * @param vs vertical coordinates for interpolation window
1650 * @param du horizontal relative coordinate
1651 * @param dv vertical relative coordinate
1653 static void xyz_to_equirect(const V360Context *s,
1654 const float *vec, int width, int height,
1655 int16_t us[4][4], int16_t vs[4][4], float *du, float *dv)
1657 const float phi = atan2f(vec[0], -vec[2]) * s->input_mirror_modifier[0];
1658 const float theta = asinf(-vec[1]) * s->input_mirror_modifier[1];
1662 uf = (phi / M_PI + 1.f) * width / 2.f;
1663 vf = (theta / M_PI_2 + 1.f) * height / 2.f;
1670 for (int i = -1; i < 3; i++) {
1671 for (int j = -1; j < 3; j++) {
1672 us[i + 1][j + 1] = mod(ui + j, width);
1673 vs[i + 1][j + 1] = av_clip(vi + i, 0, height - 1);
1679 * Prepare data for processing flat input format.
1681 * @param ctx filter context
1683 * @return error code
1685 static int prepare_flat_in(AVFilterContext *ctx)
1687 V360Context *s = ctx->priv;
1689 s->iflat_range[0] = tanf(0.5f * s->ih_fov * M_PI / 180.f);
1690 s->iflat_range[1] = tanf(0.5f * s->iv_fov * M_PI / 180.f);
1696 * Calculate frame position in flat format for corresponding 3D coordinates on sphere.
1698 * @param s filter private context
1699 * @param vec coordinates on sphere
1700 * @param width frame width
1701 * @param height frame height
1702 * @param us horizontal coordinates for interpolation window
1703 * @param vs vertical coordinates for interpolation window
1704 * @param du horizontal relative coordinate
1705 * @param dv vertical relative coordinate
1707 static void xyz_to_flat(const V360Context *s,
1708 const float *vec, int width, int height,
1709 int16_t us[4][4], int16_t vs[4][4], float *du, float *dv)
1711 const float theta = acosf(vec[2]);
1712 const float r = tanf(theta);
1713 const float rr = fabsf(r) < 1e+6f ? r : hypotf(width, height);
1714 const float zf = -vec[2];
1715 const float h = hypotf(vec[0], vec[1]);
1716 const float c = h <= 1e-6f ? 1.f : rr / h;
1717 float uf = -vec[0] * c / s->iflat_range[0] * s->input_mirror_modifier[0];
1718 float vf = vec[1] * c / s->iflat_range[1] * s->input_mirror_modifier[1];
1719 int visible, ui, vi;
1721 uf = zf >= 0.f ? (uf + 1.f) * width / 2.f : 0.f;
1722 vf = zf >= 0.f ? (vf + 1.f) * height / 2.f : 0.f;
1727 visible = vi >= 0 && vi < height && ui >= 0 && ui < width;
1732 for (int i = -1; i < 3; i++) {
1733 for (int j = -1; j < 3; j++) {
1734 us[i + 1][j + 1] = visible ? av_clip(ui + j, 0, width - 1) : 0;
1735 vs[i + 1][j + 1] = visible ? av_clip(vi + i, 0, height - 1) : 0;
1741 * Calculate frame position in mercator format for corresponding 3D coordinates on sphere.
1743 * @param s filter private context
1744 * @param vec coordinates on sphere
1745 * @param width frame width
1746 * @param height frame height
1747 * @param us horizontal coordinates for interpolation window
1748 * @param vs vertical coordinates for interpolation window
1749 * @param du horizontal relative coordinate
1750 * @param dv vertical relative coordinate
1752 static void xyz_to_mercator(const V360Context *s,
1753 const float *vec, int width, int height,
1754 int16_t us[4][4], int16_t vs[4][4], float *du, float *dv)
1756 const float phi = atan2f(vec[0], -vec[2]) * s->input_mirror_modifier[0];
1757 const float theta = -vec[1] * s->input_mirror_modifier[1];
1761 uf = (phi / M_PI + 1.f) * width / 2.f;
1762 vf = (av_clipf(logf((1.f + theta) / (1.f - theta)) / (2.f * M_PI), -1.f, 1.f) + 1.f) * height / 2.f;
1769 for (int i = -1; i < 3; i++) {
1770 for (int j = -1; j < 3; j++) {
1771 us[i + 1][j + 1] = av_clip(ui + j, 0, width - 1);
1772 vs[i + 1][j + 1] = av_clip(vi + i, 0, height - 1);
1778 * Calculate 3D coordinates on sphere for corresponding frame position in mercator format.
1780 * @param s filter private context
1781 * @param i horizontal position on frame [0, width)
1782 * @param j vertical position on frame [0, height)
1783 * @param width frame width
1784 * @param height frame height
1785 * @param vec coordinates on sphere
1787 static void mercator_to_xyz(const V360Context *s,
1788 int i, int j, int width, int height,
1791 const float phi = ((2.f * i) / width - 1.f) * M_PI + M_PI_2;
1792 const float y = ((2.f * j) / height - 1.f) * M_PI;
1793 const float div = expf(2.f * y) + 1.f;
1795 const float sin_phi = sinf(phi);
1796 const float cos_phi = cosf(phi);
1797 const float sin_theta = -2.f * expf(y) / div;
1798 const float cos_theta = -(expf(2.f * y) - 1.f) / div;
1800 vec[0] = sin_theta * cos_phi;
1802 vec[2] = sin_theta * sin_phi;
1806 * Calculate frame position in ball format for corresponding 3D coordinates on sphere.
1808 * @param s filter private context
1809 * @param vec coordinates on sphere
1810 * @param width frame width
1811 * @param height frame height
1812 * @param us horizontal coordinates for interpolation window
1813 * @param vs vertical coordinates for interpolation window
1814 * @param du horizontal relative coordinate
1815 * @param dv vertical relative coordinate
1817 static void xyz_to_ball(const V360Context *s,
1818 const float *vec, int width, int height,
1819 int16_t us[4][4], int16_t vs[4][4], float *du, float *dv)
1821 const float l = hypotf(vec[0], vec[1]);
1822 const float r = sqrtf(1.f + vec[2]) / M_SQRT2;
1826 uf = (1.f + r * vec[0] * s->input_mirror_modifier[0] / (l > 0.f ? l : 1.f)) * width * 0.5f;
1827 vf = (1.f - r * vec[1] * s->input_mirror_modifier[1] / (l > 0.f ? l : 1.f)) * height * 0.5f;
1835 for (int i = -1; i < 3; i++) {
1836 for (int j = -1; j < 3; j++) {
1837 us[i + 1][j + 1] = av_clip(ui + j, 0, width - 1);
1838 vs[i + 1][j + 1] = av_clip(vi + i, 0, height - 1);
1844 * Calculate 3D coordinates on sphere for corresponding frame position in ball format.
1846 * @param s filter private context
1847 * @param i horizontal position on frame [0, width)
1848 * @param j vertical position on frame [0, height)
1849 * @param width frame width
1850 * @param height frame height
1851 * @param vec coordinates on sphere
1853 static void ball_to_xyz(const V360Context *s,
1854 int i, int j, int width, int height,
1857 const float x = (2.f * i) / width - 1.f;
1858 const float y = (2.f * j) / height - 1.f;
1859 const float l = hypotf(x, y);
1862 const float z = 2.f * l * sqrtf(1.f - l * l);
1864 vec[0] = z * x / (l > 0.f ? l : 1.f);
1865 vec[1] = -z * y / (l > 0.f ? l : 1.f);
1866 vec[2] = -1.f + 2.f * l * l;
1875 * Calculate 3D coordinates on sphere for corresponding frame position in hammer format.
1877 * @param s filter private context
1878 * @param i horizontal position on frame [0, width)
1879 * @param j vertical position on frame [0, height)
1880 * @param width frame width
1881 * @param height frame height
1882 * @param vec coordinates on sphere
1884 static void hammer_to_xyz(const V360Context *s,
1885 int i, int j, int width, int height,
1888 const float x = ((2.f * i) / width - 1.f);
1889 const float y = ((2.f * j) / height - 1.f);
1891 const float xx = x * x;
1892 const float yy = y * y;
1894 const float z = sqrtf(1.f - xx * 0.5f - yy * 0.5f);
1896 const float a = M_SQRT2 * x * z;
1897 const float b = 2.f * z * z - 1.f;
1899 const float aa = a * a;
1900 const float bb = b * b;
1902 const float w = sqrtf(1.f - 2.f * yy * z * z);
1904 vec[0] = w * 2.f * a * b / (aa + bb);
1905 vec[1] = -M_SQRT2 * y * z;
1906 vec[2] = -w * (bb - aa) / (aa + bb);
1908 normalize_vector(vec);
1912 * Calculate frame position in hammer format for corresponding 3D coordinates on sphere.
1914 * @param s filter private context
1915 * @param vec coordinates on sphere
1916 * @param width frame width
1917 * @param height frame height
1918 * @param us horizontal coordinates for interpolation window
1919 * @param vs vertical coordinates for interpolation window
1920 * @param du horizontal relative coordinate
1921 * @param dv vertical relative coordinate
1923 static void xyz_to_hammer(const V360Context *s,
1924 const float *vec, int width, int height,
1925 int16_t us[4][4], int16_t vs[4][4], float *du, float *dv)
1927 const float theta = atan2f(vec[0], -vec[2]) * s->input_mirror_modifier[0];
1929 const float z = sqrtf(1.f + sqrtf(1.f - vec[1] * vec[1]) * cosf(theta * 0.5f));
1930 const float x = sqrtf(1.f - vec[1] * vec[1]) * sinf(theta * 0.5f) / z;
1931 const float y = -vec[1] / z * s->input_mirror_modifier[1];
1935 uf = (x + 1.f) * width / 2.f;
1936 vf = (y + 1.f) * height / 2.f;
1943 for (int i = -1; i < 3; i++) {
1944 for (int j = -1; j < 3; j++) {
1945 us[i + 1][j + 1] = av_clip(ui + j, 0, width - 1);
1946 vs[i + 1][j + 1] = av_clip(vi + i, 0, height - 1);
1952 * Calculate 3D coordinates on sphere for corresponding frame position in sinusoidal format.
1954 * @param s filter private context
1955 * @param i horizontal position on frame [0, width)
1956 * @param j vertical position on frame [0, height)
1957 * @param width frame width
1958 * @param height frame height
1959 * @param vec coordinates on sphere
1961 static void sinusoidal_to_xyz(const V360Context *s,
1962 int i, int j, int width, int height,
1965 const float theta = ((2.f * j) / height - 1.f) * M_PI_2;
1966 const float phi = ((2.f * i) / width - 1.f) * M_PI / cosf(theta);
1968 const float sin_phi = sinf(phi);
1969 const float cos_phi = cosf(phi);
1970 const float sin_theta = sinf(theta);
1971 const float cos_theta = cosf(theta);
1973 vec[0] = cos_theta * sin_phi;
1974 vec[1] = -sin_theta;
1975 vec[2] = -cos_theta * cos_phi;
1977 normalize_vector(vec);
1981 * Calculate frame position in sinusoidal format for corresponding 3D coordinates on sphere.
1983 * @param s filter private context
1984 * @param vec coordinates on sphere
1985 * @param width frame width
1986 * @param height frame height
1987 * @param us horizontal coordinates for interpolation window
1988 * @param vs vertical coordinates for interpolation window
1989 * @param du horizontal relative coordinate
1990 * @param dv vertical relative coordinate
1992 static void xyz_to_sinusoidal(const V360Context *s,
1993 const float *vec, int width, int height,
1994 int16_t us[4][4], int16_t vs[4][4], float *du, float *dv)
1996 const float theta = asinf(-vec[1]) * s->input_mirror_modifier[1];
1997 const float phi = atan2f(vec[0], -vec[2]) * s->input_mirror_modifier[0] * cosf(theta);
2001 uf = (phi / M_PI + 1.f) * width / 2.f;
2002 vf = (theta / M_PI_2 + 1.f) * height / 2.f;
2009 for (int i = -1; i < 3; i++) {
2010 for (int j = -1; j < 3; j++) {
2011 us[i + 1][j + 1] = av_clip(ui + j, 0, width - 1);
2012 vs[i + 1][j + 1] = av_clip(vi + i, 0, height - 1);
2018 * Prepare data for processing equi-angular cubemap input format.
2020 * @param ctx filter context
2022 * @return error code
2024 static int prepare_eac_in(AVFilterContext *ctx)
2026 V360Context *s = ctx->priv;
2028 if (s->ih_flip && s->iv_flip) {
2029 s->in_cubemap_face_order[RIGHT] = BOTTOM_LEFT;
2030 s->in_cubemap_face_order[LEFT] = BOTTOM_RIGHT;
2031 s->in_cubemap_face_order[UP] = TOP_LEFT;
2032 s->in_cubemap_face_order[DOWN] = TOP_RIGHT;
2033 s->in_cubemap_face_order[FRONT] = BOTTOM_MIDDLE;
2034 s->in_cubemap_face_order[BACK] = TOP_MIDDLE;
2035 } else if (s->ih_flip) {
2036 s->in_cubemap_face_order[RIGHT] = TOP_LEFT;
2037 s->in_cubemap_face_order[LEFT] = TOP_RIGHT;
2038 s->in_cubemap_face_order[UP] = BOTTOM_LEFT;
2039 s->in_cubemap_face_order[DOWN] = BOTTOM_RIGHT;
2040 s->in_cubemap_face_order[FRONT] = TOP_MIDDLE;
2041 s->in_cubemap_face_order[BACK] = BOTTOM_MIDDLE;
2042 } else if (s->iv_flip) {
2043 s->in_cubemap_face_order[RIGHT] = BOTTOM_RIGHT;
2044 s->in_cubemap_face_order[LEFT] = BOTTOM_LEFT;
2045 s->in_cubemap_face_order[UP] = TOP_RIGHT;
2046 s->in_cubemap_face_order[DOWN] = TOP_LEFT;
2047 s->in_cubemap_face_order[FRONT] = BOTTOM_MIDDLE;
2048 s->in_cubemap_face_order[BACK] = TOP_MIDDLE;
2050 s->in_cubemap_face_order[RIGHT] = TOP_RIGHT;
2051 s->in_cubemap_face_order[LEFT] = TOP_LEFT;
2052 s->in_cubemap_face_order[UP] = BOTTOM_RIGHT;
2053 s->in_cubemap_face_order[DOWN] = BOTTOM_LEFT;
2054 s->in_cubemap_face_order[FRONT] = TOP_MIDDLE;
2055 s->in_cubemap_face_order[BACK] = BOTTOM_MIDDLE;
2059 s->in_cubemap_face_rotation[TOP_LEFT] = ROT_270;
2060 s->in_cubemap_face_rotation[TOP_MIDDLE] = ROT_90;
2061 s->in_cubemap_face_rotation[TOP_RIGHT] = ROT_270;
2062 s->in_cubemap_face_rotation[BOTTOM_LEFT] = ROT_0;
2063 s->in_cubemap_face_rotation[BOTTOM_MIDDLE] = ROT_0;
2064 s->in_cubemap_face_rotation[BOTTOM_RIGHT] = ROT_0;
2066 s->in_cubemap_face_rotation[TOP_LEFT] = ROT_0;
2067 s->in_cubemap_face_rotation[TOP_MIDDLE] = ROT_0;
2068 s->in_cubemap_face_rotation[TOP_RIGHT] = ROT_0;
2069 s->in_cubemap_face_rotation[BOTTOM_LEFT] = ROT_270;
2070 s->in_cubemap_face_rotation[BOTTOM_MIDDLE] = ROT_90;
2071 s->in_cubemap_face_rotation[BOTTOM_RIGHT] = ROT_270;
2078 * Prepare data for processing equi-angular cubemap output format.
2080 * @param ctx filter context
2082 * @return error code
2084 static int prepare_eac_out(AVFilterContext *ctx)
2086 V360Context *s = ctx->priv;
2088 s->out_cubemap_direction_order[TOP_LEFT] = LEFT;
2089 s->out_cubemap_direction_order[TOP_MIDDLE] = FRONT;
2090 s->out_cubemap_direction_order[TOP_RIGHT] = RIGHT;
2091 s->out_cubemap_direction_order[BOTTOM_LEFT] = DOWN;
2092 s->out_cubemap_direction_order[BOTTOM_MIDDLE] = BACK;
2093 s->out_cubemap_direction_order[BOTTOM_RIGHT] = UP;
2095 s->out_cubemap_face_rotation[TOP_LEFT] = ROT_0;
2096 s->out_cubemap_face_rotation[TOP_MIDDLE] = ROT_0;
2097 s->out_cubemap_face_rotation[TOP_RIGHT] = ROT_0;
2098 s->out_cubemap_face_rotation[BOTTOM_LEFT] = ROT_270;
2099 s->out_cubemap_face_rotation[BOTTOM_MIDDLE] = ROT_90;
2100 s->out_cubemap_face_rotation[BOTTOM_RIGHT] = ROT_270;
2106 * Calculate 3D coordinates on sphere for corresponding frame position in equi-angular cubemap format.
2108 * @param s filter private context
2109 * @param i horizontal position on frame [0, width)
2110 * @param j vertical position on frame [0, height)
2111 * @param width frame width
2112 * @param height frame height
2113 * @param vec coordinates on sphere
2115 static void eac_to_xyz(const V360Context *s,
2116 int i, int j, int width, int height,
2119 const float pixel_pad = 2;
2120 const float u_pad = pixel_pad / width;
2121 const float v_pad = pixel_pad / height;
2123 int u_face, v_face, face;
2125 float l_x, l_y, l_z;
2127 float uf = (i + 0.5f) / width;
2128 float vf = (j + 0.5f) / height;
2130 // EAC has 2-pixel padding on faces except between faces on the same row
2131 // Padding pixels seems not to be stretched with tangent as regular pixels
2132 // Formulas below approximate original padding as close as I could get experimentally
2134 // Horizontal padding
2135 uf = 3.f * (uf - u_pad) / (1.f - 2.f * u_pad);
2139 } else if (uf >= 3.f) {
2143 u_face = floorf(uf);
2144 uf = fmodf(uf, 1.f) - 0.5f;
2148 v_face = floorf(vf * 2.f);
2149 vf = (vf - v_pad - 0.5f * v_face) / (0.5f - 2.f * v_pad) - 0.5f;
2151 if (uf >= -0.5f && uf < 0.5f) {
2152 uf = tanf(M_PI_2 * uf);
2156 if (vf >= -0.5f && vf < 0.5f) {
2157 vf = tanf(M_PI_2 * vf);
2162 face = u_face + 3 * v_face;
2203 normalize_vector(vec);
2207 * Calculate frame position in equi-angular cubemap format for corresponding 3D coordinates on sphere.
2209 * @param s filter private context
2210 * @param vec coordinates on sphere
2211 * @param width frame width
2212 * @param height frame height
2213 * @param us horizontal coordinates for interpolation window
2214 * @param vs vertical coordinates for interpolation window
2215 * @param du horizontal relative coordinate
2216 * @param dv vertical relative coordinate
2218 static void xyz_to_eac(const V360Context *s,
2219 const float *vec, int width, int height,
2220 int16_t us[4][4], int16_t vs[4][4], float *du, float *dv)
2222 const float pixel_pad = 2;
2223 const float u_pad = pixel_pad / width;
2224 const float v_pad = pixel_pad / height;
2228 int direction, face;
2231 xyz_to_cube(s, vec, &uf, &vf, &direction);
2233 face = s->in_cubemap_face_order[direction];
2237 uf = M_2_PI * atanf(uf) + 0.5f;
2238 vf = M_2_PI * atanf(vf) + 0.5f;
2240 // These formulas are inversed from eac_to_xyz ones
2241 uf = (uf + u_face) * (1.f - 2.f * u_pad) / 3.f + u_pad;
2242 vf = vf * (0.5f - 2.f * v_pad) + v_pad + 0.5f * v_face;
2256 for (int i = -1; i < 3; i++) {
2257 for (int j = -1; j < 3; j++) {
2258 us[i + 1][j + 1] = av_clip(ui + j, 0, width - 1);
2259 vs[i + 1][j + 1] = av_clip(vi + i, 0, height - 1);
2265 * Prepare data for processing flat output format.
2267 * @param ctx filter context
2269 * @return error code
2271 static int prepare_flat_out(AVFilterContext *ctx)
2273 V360Context *s = ctx->priv;
2275 s->flat_range[0] = tanf(0.5f * s->h_fov * M_PI / 180.f);
2276 s->flat_range[1] = tanf(0.5f * s->v_fov * M_PI / 180.f);
2282 * Calculate 3D coordinates on sphere for corresponding frame position in flat format.
2284 * @param s filter private context
2285 * @param i horizontal position on frame [0, width)
2286 * @param j vertical position on frame [0, height)
2287 * @param width frame width
2288 * @param height frame height
2289 * @param vec coordinates on sphere
2291 static void flat_to_xyz(const V360Context *s,
2292 int i, int j, int width, int height,
2295 const float l_x = s->flat_range[0] * (2.f * i / width - 1.f);
2296 const float l_y = -s->flat_range[1] * (2.f * j / height - 1.f);
2302 normalize_vector(vec);
2306 * Prepare data for processing fisheye output format.
2308 * @param ctx filter context
2310 * @return error code
2312 static int prepare_fisheye_out(AVFilterContext *ctx)
2314 V360Context *s = ctx->priv;
2316 s->flat_range[0] = s->h_fov / 180.f;
2317 s->flat_range[1] = s->v_fov / 180.f;
2323 * Calculate 3D coordinates on sphere for corresponding frame position in fisheye format.
2325 * @param s filter private context
2326 * @param i horizontal position on frame [0, width)
2327 * @param j vertical position on frame [0, height)
2328 * @param width frame width
2329 * @param height frame height
2330 * @param vec coordinates on sphere
2332 static void fisheye_to_xyz(const V360Context *s,
2333 int i, int j, int width, int height,
2336 const float uf = s->flat_range[0] * ((2.f * i) / width - 1.f);
2337 const float vf = s->flat_range[1] * ((2.f * j) / height - 1.f);
2339 const float phi = -atan2f(vf, uf);
2340 const float theta = -M_PI_2 * (1.f - hypotf(uf, vf));
2342 vec[0] = cosf(theta) * cosf(phi);
2343 vec[1] = cosf(theta) * sinf(phi);
2344 vec[2] = sinf(theta);
2346 normalize_vector(vec);
2350 * Prepare data for processing fisheye input format.
2352 * @param ctx filter context
2354 * @return error code
2356 static int prepare_fisheye_in(AVFilterContext *ctx)
2358 V360Context *s = ctx->priv;
2360 s->iflat_range[0] = s->ih_fov / 180.f;
2361 s->iflat_range[1] = s->iv_fov / 180.f;
2367 * Calculate frame position in fisheye format for corresponding 3D coordinates on sphere.
2369 * @param s filter private context
2370 * @param vec coordinates on sphere
2371 * @param width frame width
2372 * @param height frame height
2373 * @param us horizontal coordinates for interpolation window
2374 * @param vs vertical coordinates for interpolation window
2375 * @param du horizontal relative coordinate
2376 * @param dv vertical relative coordinate
2378 static void xyz_to_fisheye(const V360Context *s,
2379 const float *vec, int width, int height,
2380 int16_t us[4][4], int16_t vs[4][4], float *du, float *dv)
2382 const float h = hypotf(vec[0], vec[1]);
2383 const float lh = h > 0.f ? h : 1.f;
2384 const float theta = acosf(fabsf(vec[2])) / M_PI;
2386 const float uf = (theta * ( vec[0] / lh) * s->input_mirror_modifier[0] / s->iflat_range[0] + 0.5f) * width;
2387 const float vf = (theta * (-vec[1] / lh) * s->input_mirror_modifier[1] / s->iflat_range[1] + 0.5f) * height;
2389 int visible, ui, vi;
2394 visible = vec[2] < 0.f;
2399 for (int i = -1; i < 3; i++) {
2400 for (int j = -1; j < 3; j++) {
2401 us[i + 1][j + 1] = visible ? av_clip(ui + j, 0, width - 1) : 0;
2402 vs[i + 1][j + 1] = visible ? av_clip(vi + i, 0, height - 1) : 0;
2408 * Calculate 3D coordinates on sphere for corresponding frame position in pannini format.
2410 * @param s filter private context
2411 * @param i horizontal position on frame [0, width)
2412 * @param j vertical position on frame [0, height)
2413 * @param width frame width
2414 * @param height frame height
2415 * @param vec coordinates on sphere
2417 static void pannini_to_xyz(const V360Context *s,
2418 int i, int j, int width, int height,
2421 const float uf = ((2.f * i) / width - 1.f);
2422 const float vf = ((2.f * j) / height - 1.f);
2424 const float d = s->h_fov;
2425 const float k = uf * uf / ((d + 1.f) * (d + 1.f));
2426 const float dscr = k * k * d * d - (k + 1.f) * (k * d * d - 1.f);
2427 const float clon = (-k * d + sqrtf(dscr)) / (k + 1.f);
2428 const float S = (d + 1.f) / (d + clon);
2429 const float lon = -(M_PI + atan2f(uf, S * clon));
2430 const float lat = -atan2f(vf, S);
2432 vec[0] = sinf(lon) * cosf(lat);
2434 vec[2] = cosf(lon) * cosf(lat);
2436 normalize_vector(vec);
2440 * Prepare data for processing cylindrical output format.
2442 * @param ctx filter context
2444 * @return error code
2446 static int prepare_cylindrical_out(AVFilterContext *ctx)
2448 V360Context *s = ctx->priv;
2450 s->flat_range[0] = M_PI * s->h_fov / 360.f;
2451 s->flat_range[1] = tanf(0.5f * s->v_fov * M_PI / 180.f);
2457 * Calculate 3D coordinates on sphere for corresponding frame position in cylindrical format.
2459 * @param s filter private context
2460 * @param i horizontal position on frame [0, width)
2461 * @param j vertical position on frame [0, height)
2462 * @param width frame width
2463 * @param height frame height
2464 * @param vec coordinates on sphere
2466 static void cylindrical_to_xyz(const V360Context *s,
2467 int i, int j, int width, int height,
2470 const float uf = s->flat_range[0] * ((2.f * i) / width - 1.f);
2471 const float vf = s->flat_range[1] * ((2.f * j) / height - 1.f);
2473 const float phi = uf;
2474 const float theta = atanf(vf);
2476 const float sin_phi = sinf(phi);
2477 const float cos_phi = cosf(phi);
2478 const float sin_theta = sinf(theta);
2479 const float cos_theta = cosf(theta);
2481 vec[0] = cos_theta * sin_phi;
2482 vec[1] = -sin_theta;
2483 vec[2] = -cos_theta * cos_phi;
2485 normalize_vector(vec);
2489 * Prepare data for processing cylindrical input format.
2491 * @param ctx filter context
2493 * @return error code
2495 static int prepare_cylindrical_in(AVFilterContext *ctx)
2497 V360Context *s = ctx->priv;
2499 s->iflat_range[0] = M_PI * s->ih_fov / 360.f;
2500 s->iflat_range[1] = tanf(0.5f * s->iv_fov * M_PI / 180.f);
2506 * Calculate frame position in cylindrical format for corresponding 3D coordinates on sphere.
2508 * @param s filter private context
2509 * @param vec coordinates on sphere
2510 * @param width frame width
2511 * @param height frame height
2512 * @param us horizontal coordinates for interpolation window
2513 * @param vs vertical coordinates for interpolation window
2514 * @param du horizontal relative coordinate
2515 * @param dv vertical relative coordinate
2517 static void xyz_to_cylindrical(const V360Context *s,
2518 const float *vec, int width, int height,
2519 int16_t us[4][4], int16_t vs[4][4], float *du, float *dv)
2521 const float phi = atan2f(vec[0], -vec[2]) * s->input_mirror_modifier[0] / s->iflat_range[0];
2522 const float theta = atan2f(-vec[1], hypotf(vec[0], vec[2])) * s->input_mirror_modifier[1] / s->iflat_range[1];
2523 int visible, ui, vi;
2526 uf = (phi + 1.f) * (width - 1) / 2.f;
2527 vf = (tanf(theta) + 1.f) * height / 2.f;
2531 visible = vi >= 0 && vi < height && ui >= 0 && ui < width &&
2532 theta <= M_PI * s->iv_fov / 180.f &&
2533 theta >= -M_PI * s->iv_fov / 180.f;
2538 for (int i = -1; i < 3; i++) {
2539 for (int j = -1; j < 3; j++) {
2540 us[i + 1][j + 1] = visible ? av_clip(ui + j, 0, width - 1) : 0;
2541 vs[i + 1][j + 1] = visible ? av_clip(vi + i, 0, height - 1) : 0;
2547 * Calculate 3D coordinates on sphere for corresponding frame position in perspective format.
2549 * @param s filter private context
2550 * @param i horizontal position on frame [0, width)
2551 * @param j vertical position on frame [0, height)
2552 * @param width frame width
2553 * @param height frame height
2554 * @param vec coordinates on sphere
2556 static void perspective_to_xyz(const V360Context *s,
2557 int i, int j, int width, int height,
2560 const float uf = ((2.f * i) / width - 1.f);
2561 const float vf = ((2.f * j) / height - 1.f);
2562 const float rh = hypotf(uf, vf);
2563 const float sinzz = 1.f - rh * rh;
2564 const float h = 1.f + s->v_fov;
2565 const float sinz = (h - sqrtf(sinzz)) / (h / rh + rh / h);
2566 const float sinz2 = sinz * sinz;
2569 const float cosz = sqrtf(1.f - sinz2);
2571 const float theta = asinf(cosz);
2572 const float phi = atan2f(uf, vf);
2574 const float sin_phi = sinf(phi);
2575 const float cos_phi = cosf(phi);
2576 const float sin_theta = sinf(theta);
2577 const float cos_theta = cosf(theta);
2579 vec[0] = cos_theta * sin_phi;
2581 vec[2] = -cos_theta * cos_phi;
2588 normalize_vector(vec);
2592 * Calculate 3D coordinates on sphere for corresponding frame position in dual fisheye format.
2594 * @param s filter private context
2595 * @param i horizontal position on frame [0, width)
2596 * @param j vertical position on frame [0, height)
2597 * @param width frame width
2598 * @param height frame height
2599 * @param vec coordinates on sphere
2601 static void dfisheye_to_xyz(const V360Context *s,
2602 int i, int j, int width, int height,
2605 const float scale = 1.f + s->out_pad;
2607 const float ew = width / 2.f;
2608 const float eh = height;
2610 const int ei = i >= ew ? i - ew : i;
2611 const float m = i >= ew ? -1.f : 1.f;
2613 const float uf = ((2.f * ei) / ew - 1.f) * scale;
2614 const float vf = ((2.f * j) / eh - 1.f) * scale;
2616 const float h = hypotf(uf, vf);
2617 const float lh = h > 0.f ? h : 1.f;
2618 const float theta = m * M_PI_2 * (1.f - h);
2620 const float sin_theta = sinf(theta);
2621 const float cos_theta = cosf(theta);
2623 vec[0] = cos_theta * m * -uf / lh;
2624 vec[1] = cos_theta * -vf / lh;
2627 normalize_vector(vec);
2631 * Calculate frame position in dual fisheye format for corresponding 3D coordinates on sphere.
2633 * @param s filter private context
2634 * @param vec coordinates on sphere
2635 * @param width frame width
2636 * @param height frame height
2637 * @param us horizontal coordinates for interpolation window
2638 * @param vs vertical coordinates for interpolation window
2639 * @param du horizontal relative coordinate
2640 * @param dv vertical relative coordinate
2642 static void xyz_to_dfisheye(const V360Context *s,
2643 const float *vec, int width, int height,
2644 int16_t us[4][4], int16_t vs[4][4], float *du, float *dv)
2646 const float scale = 1.f - s->in_pad;
2648 const float ew = width / 2.f;
2649 const float eh = height;
2651 const float h = hypotf(vec[0], vec[1]);
2652 const float lh = h > 0.f ? h : 1.f;
2653 const float theta = acosf(fabsf(vec[2])) / M_PI;
2655 float uf = (theta * (-vec[0] / lh) * s->input_mirror_modifier[0] * scale + 0.5f) * ew;
2656 float vf = (theta * (-vec[1] / lh) * s->input_mirror_modifier[1] * scale + 0.5f) * eh;
2661 if (vec[2] >= 0.f) {
2664 u_shift = ceilf(ew);
2674 for (int i = -1; i < 3; i++) {
2675 for (int j = -1; j < 3; j++) {
2676 us[i + 1][j + 1] = av_clip(u_shift + ui + j, 0, width - 1);
2677 vs[i + 1][j + 1] = av_clip( vi + i, 0, height - 1);
2683 * Calculate 3D coordinates on sphere for corresponding frame position in barrel facebook's format.
2685 * @param s filter private context
2686 * @param i horizontal position on frame [0, width)
2687 * @param j vertical position on frame [0, height)
2688 * @param width frame width
2689 * @param height frame height
2690 * @param vec coordinates on sphere
2692 static void barrel_to_xyz(const V360Context *s,
2693 int i, int j, int width, int height,
2696 const float scale = 0.99f;
2697 float l_x, l_y, l_z;
2699 if (i < 4 * width / 5) {
2700 const float theta_range = M_PI_4;
2702 const int ew = 4 * width / 5;
2703 const int eh = height;
2705 const float phi = ((2.f * i) / ew - 1.f) * M_PI / scale;
2706 const float theta = ((2.f * j) / eh - 1.f) * theta_range / scale;
2708 const float sin_phi = sinf(phi);
2709 const float cos_phi = cosf(phi);
2710 const float sin_theta = sinf(theta);
2711 const float cos_theta = cosf(theta);
2713 l_x = cos_theta * sin_phi;
2715 l_z = -cos_theta * cos_phi;
2717 const int ew = width / 5;
2718 const int eh = height / 2;
2723 uf = 2.f * (i - 4 * ew) / ew - 1.f;
2724 vf = 2.f * (j ) / eh - 1.f;
2733 uf = 2.f * (i - 4 * ew) / ew - 1.f;
2734 vf = 2.f * (j - eh) / eh - 1.f;
2749 normalize_vector(vec);
2753 * Calculate frame position in barrel facebook's format for corresponding 3D coordinates on sphere.
2755 * @param s filter private context
2756 * @param vec coordinates on sphere
2757 * @param width frame width
2758 * @param height frame height
2759 * @param us horizontal coordinates for interpolation window
2760 * @param vs vertical coordinates for interpolation window
2761 * @param du horizontal relative coordinate
2762 * @param dv vertical relative coordinate
2764 static void xyz_to_barrel(const V360Context *s,
2765 const float *vec, int width, int height,
2766 int16_t us[4][4], int16_t vs[4][4], float *du, float *dv)
2768 const float scale = 0.99f;
2770 const float phi = atan2f(vec[0], -vec[2]) * s->input_mirror_modifier[0];
2771 const float theta = asinf(-vec[1]) * s->input_mirror_modifier[1];
2772 const float theta_range = M_PI_4;
2775 int u_shift, v_shift;
2779 if (theta > -theta_range && theta < theta_range) {
2783 u_shift = s->ih_flip ? width / 5 : 0;
2786 uf = (phi / M_PI * scale + 1.f) * ew / 2.f;
2787 vf = (theta / theta_range * scale + 1.f) * eh / 2.f;
2792 u_shift = s->ih_flip ? 0 : 4 * ew;
2794 if (theta < 0.f) { // UP
2795 uf = vec[0] / vec[1];
2796 vf = -vec[2] / vec[1];
2799 uf = -vec[0] / vec[1];
2800 vf = -vec[2] / vec[1];
2804 uf *= s->input_mirror_modifier[0] * s->input_mirror_modifier[1];
2805 vf *= s->input_mirror_modifier[1];
2807 uf = 0.5f * ew * (uf * scale + 1.f);
2808 vf = 0.5f * eh * (vf * scale + 1.f);
2817 for (int i = -1; i < 3; i++) {
2818 for (int j = -1; j < 3; j++) {
2819 us[i + 1][j + 1] = u_shift + av_clip(ui + j, 0, ew - 1);
2820 vs[i + 1][j + 1] = v_shift + av_clip(vi + i, 0, eh - 1);
2825 static void multiply_matrix(float c[3][3], const float a[3][3], const float b[3][3])
2827 for (int i = 0; i < 3; i++) {
2828 for (int j = 0; j < 3; j++) {
2831 for (int k = 0; k < 3; k++)
2832 sum += a[i][k] * b[k][j];
2840 * Calculate rotation matrix for yaw/pitch/roll angles.
2842 static inline void calculate_rotation_matrix(float yaw, float pitch, float roll,
2843 float rot_mat[3][3],
2844 const int rotation_order[3])
2846 const float yaw_rad = yaw * M_PI / 180.f;
2847 const float pitch_rad = pitch * M_PI / 180.f;
2848 const float roll_rad = roll * M_PI / 180.f;
2850 const float sin_yaw = sinf(-yaw_rad);
2851 const float cos_yaw = cosf(-yaw_rad);
2852 const float sin_pitch = sinf(pitch_rad);
2853 const float cos_pitch = cosf(pitch_rad);
2854 const float sin_roll = sinf(roll_rad);
2855 const float cos_roll = cosf(roll_rad);
2860 m[0][0][0] = cos_yaw; m[0][0][1] = 0; m[0][0][2] = sin_yaw;
2861 m[0][1][0] = 0; m[0][1][1] = 1; m[0][1][2] = 0;
2862 m[0][2][0] = -sin_yaw; m[0][2][1] = 0; m[0][2][2] = cos_yaw;
2864 m[1][0][0] = 1; m[1][0][1] = 0; m[1][0][2] = 0;
2865 m[1][1][0] = 0; m[1][1][1] = cos_pitch; m[1][1][2] = -sin_pitch;
2866 m[1][2][0] = 0; m[1][2][1] = sin_pitch; m[1][2][2] = cos_pitch;
2868 m[2][0][0] = cos_roll; m[2][0][1] = -sin_roll; m[2][0][2] = 0;
2869 m[2][1][0] = sin_roll; m[2][1][1] = cos_roll; m[2][1][2] = 0;
2870 m[2][2][0] = 0; m[2][2][1] = 0; m[2][2][2] = 1;
2872 multiply_matrix(temp, m[rotation_order[0]], m[rotation_order[1]]);
2873 multiply_matrix(rot_mat, temp, m[rotation_order[2]]);
2877 * Rotate vector with given rotation matrix.
2879 * @param rot_mat rotation matrix
2882 static inline void rotate(const float rot_mat[3][3],
2885 const float x_tmp = vec[0] * rot_mat[0][0] + vec[1] * rot_mat[0][1] + vec[2] * rot_mat[0][2];
2886 const float y_tmp = vec[0] * rot_mat[1][0] + vec[1] * rot_mat[1][1] + vec[2] * rot_mat[1][2];
2887 const float z_tmp = vec[0] * rot_mat[2][0] + vec[1] * rot_mat[2][1] + vec[2] * rot_mat[2][2];
2894 static inline void set_mirror_modifier(int h_flip, int v_flip, int d_flip,
2897 modifier[0] = h_flip ? -1.f : 1.f;
2898 modifier[1] = v_flip ? -1.f : 1.f;
2899 modifier[2] = d_flip ? -1.f : 1.f;
2902 static inline void mirror(const float *modifier, float *vec)
2904 vec[0] *= modifier[0];
2905 vec[1] *= modifier[1];
2906 vec[2] *= modifier[2];
2909 static int allocate_plane(V360Context *s, int sizeof_uv, int sizeof_ker, int p)
2911 s->u[p] = av_calloc(s->uv_linesize[p] * s->pr_height[p], sizeof_uv);
2912 s->v[p] = av_calloc(s->uv_linesize[p] * s->pr_height[p], sizeof_uv);
2913 if (!s->u[p] || !s->v[p])
2914 return AVERROR(ENOMEM);
2916 s->ker[p] = av_calloc(s->uv_linesize[p] * s->pr_height[p], sizeof_ker);
2918 return AVERROR(ENOMEM);
2924 static void fov_from_dfov(float d_fov, float w, float h, float *h_fov, float *v_fov)
2926 const float da = tanf(0.5 * FFMIN(d_fov, 359.f) * M_PI / 180.f);
2927 const float d = hypotf(w, h);
2929 *h_fov = atan2f(da * w, d) * 360.f / M_PI;
2930 *v_fov = atan2f(da * h, d) * 360.f / M_PI;
2938 static void set_dimensions(int *outw, int *outh, int w, int h, const AVPixFmtDescriptor *desc)
2940 outw[1] = outw[2] = FF_CEIL_RSHIFT(w, desc->log2_chroma_w);
2941 outw[0] = outw[3] = w;
2942 outh[1] = outh[2] = FF_CEIL_RSHIFT(h, desc->log2_chroma_h);
2943 outh[0] = outh[3] = h;
2946 // Calculate remap data
2947 static av_always_inline int v360_slice(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
2949 V360Context *s = ctx->priv;
2951 for (int p = 0; p < s->nb_allocated; p++) {
2952 const int width = s->pr_width[p];
2953 const int uv_linesize = s->uv_linesize[p];
2954 const int height = s->pr_height[p];
2955 const int in_width = s->inplanewidth[p];
2956 const int in_height = s->inplaneheight[p];
2957 const int slice_start = (height * jobnr ) / nb_jobs;
2958 const int slice_end = (height * (jobnr + 1)) / nb_jobs;
2963 for (int j = slice_start; j < slice_end; j++) {
2964 for (int i = 0; i < width; i++) {
2965 int16_t *u = s->u[p] + (j * uv_linesize + i) * s->elements;
2966 int16_t *v = s->v[p] + (j * uv_linesize + i) * s->elements;
2967 int16_t *ker = s->ker[p] + (j * uv_linesize + i) * s->elements;
2969 if (s->out_transpose)
2970 s->out_transform(s, j, i, height, width, vec);
2972 s->out_transform(s, i, j, width, height, vec);
2973 av_assert1(!isnan(vec[0]) && !isnan(vec[1]) && !isnan(vec[2]));
2974 rotate(s->rot_mat, vec);
2975 av_assert1(!isnan(vec[0]) && !isnan(vec[1]) && !isnan(vec[2]));
2976 normalize_vector(vec);
2977 mirror(s->output_mirror_modifier, vec);
2978 if (s->in_transpose)
2979 s->in_transform(s, vec, in_height, in_width, rmap.v, rmap.u, &du, &dv);
2981 s->in_transform(s, vec, in_width, in_height, rmap.u, rmap.v, &du, &dv);
2982 av_assert1(!isnan(du) && !isnan(dv));
2983 s->calculate_kernel(du, dv, &rmap, u, v, ker);
2991 static int config_output(AVFilterLink *outlink)
2993 AVFilterContext *ctx = outlink->src;
2994 AVFilterLink *inlink = ctx->inputs[0];
2995 V360Context *s = ctx->priv;
2996 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(inlink->format);
2997 const int depth = desc->comp[0].depth;
3002 int in_offset_h, in_offset_w;
3003 int out_offset_h, out_offset_w;
3005 int (*prepare_out)(AVFilterContext *ctx);
3007 s->input_mirror_modifier[0] = s->ih_flip ? -1.f : 1.f;
3008 s->input_mirror_modifier[1] = s->iv_flip ? -1.f : 1.f;
3010 switch (s->interp) {
3012 s->calculate_kernel = nearest_kernel;
3013 s->remap_slice = depth <= 8 ? remap1_8bit_slice : remap1_16bit_slice;
3015 sizeof_uv = sizeof(int16_t) * s->elements;
3019 s->calculate_kernel = bilinear_kernel;
3020 s->remap_slice = depth <= 8 ? remap2_8bit_slice : remap2_16bit_slice;
3021 s->elements = 2 * 2;
3022 sizeof_uv = sizeof(int16_t) * s->elements;
3023 sizeof_ker = sizeof(int16_t) * s->elements;
3026 s->calculate_kernel = bicubic_kernel;
3027 s->remap_slice = depth <= 8 ? remap4_8bit_slice : remap4_16bit_slice;
3028 s->elements = 4 * 4;
3029 sizeof_uv = sizeof(int16_t) * s->elements;
3030 sizeof_ker = sizeof(int16_t) * s->elements;
3033 s->calculate_kernel = lanczos_kernel;
3034 s->remap_slice = depth <= 8 ? remap4_8bit_slice : remap4_16bit_slice;
3035 s->elements = 4 * 4;
3036 sizeof_uv = sizeof(int16_t) * s->elements;
3037 sizeof_ker = sizeof(int16_t) * s->elements;
3040 s->calculate_kernel = spline16_kernel;
3041 s->remap_slice = depth <= 8 ? remap4_8bit_slice : remap4_16bit_slice;
3042 s->elements = 4 * 4;
3043 sizeof_uv = sizeof(int16_t) * s->elements;
3044 sizeof_ker = sizeof(int16_t) * s->elements;
3047 s->calculate_kernel = gaussian_kernel;
3048 s->remap_slice = depth <= 8 ? remap4_8bit_slice : remap4_16bit_slice;
3049 s->elements = 4 * 4;
3050 sizeof_uv = sizeof(int16_t) * s->elements;
3051 sizeof_ker = sizeof(int16_t) * s->elements;
3057 ff_v360_init(s, depth);
3059 for (int order = 0; order < NB_RORDERS; order++) {
3060 const char c = s->rorder[order];
3064 av_log(ctx, AV_LOG_ERROR,
3065 "Incomplete rorder option. Direction for all 3 rotation orders should be specified.\n");
3066 return AVERROR(EINVAL);
3069 rorder = get_rorder(c);
3071 av_log(ctx, AV_LOG_ERROR,
3072 "Incorrect rotation order symbol '%c' in rorder option.\n", c);
3073 return AVERROR(EINVAL);
3076 s->rotation_order[order] = rorder;
3079 switch (s->in_stereo) {
3083 in_offset_w = in_offset_h = 0;
3101 set_dimensions(s->inplanewidth, s->inplaneheight, w, h, desc);
3102 set_dimensions(s->in_offset_w, s->in_offset_h, in_offset_w, in_offset_h, desc);
3104 s->in_width = s->inplanewidth[0];
3105 s->in_height = s->inplaneheight[0];
3107 if (s->id_fov > 0.f)
3108 fov_from_dfov(s->id_fov, w, h, &s->ih_fov, &s->iv_fov);
3110 if (s->in_transpose)
3111 FFSWAP(int, s->in_width, s->in_height);
3114 case EQUIRECTANGULAR:
3115 s->in_transform = xyz_to_equirect;
3121 s->in_transform = xyz_to_cube3x2;
3122 err = prepare_cube_in(ctx);
3127 s->in_transform = xyz_to_cube1x6;
3128 err = prepare_cube_in(ctx);
3133 s->in_transform = xyz_to_cube6x1;
3134 err = prepare_cube_in(ctx);
3139 s->in_transform = xyz_to_eac;
3140 err = prepare_eac_in(ctx);
3145 s->in_transform = xyz_to_flat;
3146 err = prepare_flat_in(ctx);
3152 av_log(ctx, AV_LOG_ERROR, "Supplied format is not accepted as input.\n");
3153 return AVERROR(EINVAL);
3155 s->in_transform = xyz_to_dfisheye;
3161 s->in_transform = xyz_to_barrel;
3167 s->in_transform = xyz_to_stereographic;
3168 err = prepare_stereographic_in(ctx);
3173 s->in_transform = xyz_to_mercator;
3179 s->in_transform = xyz_to_ball;
3185 s->in_transform = xyz_to_hammer;
3191 s->in_transform = xyz_to_sinusoidal;
3197 s->in_transform = xyz_to_fisheye;
3198 err = prepare_fisheye_in(ctx);
3203 s->in_transform = xyz_to_cylindrical;
3204 err = prepare_cylindrical_in(ctx);
3209 av_log(ctx, AV_LOG_ERROR, "Specified input format is not handled.\n");
3218 case EQUIRECTANGULAR:
3219 s->out_transform = equirect_to_xyz;
3225 s->out_transform = cube3x2_to_xyz;
3226 prepare_out = prepare_cube_out;
3227 w = lrintf(wf / 4.f * 3.f);
3231 s->out_transform = cube1x6_to_xyz;
3232 prepare_out = prepare_cube_out;
3233 w = lrintf(wf / 4.f);
3234 h = lrintf(hf * 3.f);
3237 s->out_transform = cube6x1_to_xyz;
3238 prepare_out = prepare_cube_out;
3239 w = lrintf(wf / 2.f * 3.f);
3240 h = lrintf(hf / 2.f);
3243 s->out_transform = eac_to_xyz;
3244 prepare_out = prepare_eac_out;
3246 h = lrintf(hf / 8.f * 9.f);
3249 s->out_transform = flat_to_xyz;
3250 prepare_out = prepare_flat_out;
3255 s->out_transform = dfisheye_to_xyz;
3261 s->out_transform = barrel_to_xyz;
3263 w = lrintf(wf / 4.f * 5.f);
3267 s->out_transform = stereographic_to_xyz;
3268 prepare_out = prepare_stereographic_out;
3270 h = lrintf(hf * 2.f);
3273 s->out_transform = mercator_to_xyz;
3276 h = lrintf(hf * 2.f);
3279 s->out_transform = ball_to_xyz;
3282 h = lrintf(hf * 2.f);
3285 s->out_transform = hammer_to_xyz;
3291 s->out_transform = sinusoidal_to_xyz;
3297 s->out_transform = fisheye_to_xyz;
3298 prepare_out = prepare_fisheye_out;
3299 w = lrintf(wf * 0.5f);
3303 s->out_transform = pannini_to_xyz;
3309 s->out_transform = cylindrical_to_xyz;
3310 prepare_out = prepare_cylindrical_out;
3312 h = lrintf(hf * 0.5f);
3315 s->out_transform = perspective_to_xyz;
3317 w = lrintf(wf / 2.f);
3321 av_log(ctx, AV_LOG_ERROR, "Specified output format is not handled.\n");
3325 // Override resolution with user values if specified
3326 if (s->width > 0 && s->height > 0) {
3329 } else if (s->width > 0 || s->height > 0) {
3330 av_log(ctx, AV_LOG_ERROR, "Both width and height values should be specified.\n");
3331 return AVERROR(EINVAL);
3333 if (s->out_transpose)
3336 if (s->in_transpose)
3341 fov_from_dfov(s->d_fov, w, h, &s->h_fov, &s->v_fov);
3344 err = prepare_out(ctx);
3349 set_dimensions(s->pr_width, s->pr_height, w, h, desc);
3351 s->out_width = s->pr_width[0];
3352 s->out_height = s->pr_height[0];
3354 if (s->out_transpose)
3355 FFSWAP(int, s->out_width, s->out_height);
3357 switch (s->out_stereo) {
3359 out_offset_w = out_offset_h = 0;
3375 set_dimensions(s->out_offset_w, s->out_offset_h, out_offset_w, out_offset_h, desc);
3376 set_dimensions(s->planewidth, s->planeheight, w, h, desc);
3378 for (int i = 0; i < 4; i++)
3379 s->uv_linesize[i] = FFALIGN(s->pr_width[i], 8);
3384 s->nb_planes = av_pix_fmt_count_planes(inlink->format);
3386 if (desc->log2_chroma_h == desc->log2_chroma_w && desc->log2_chroma_h == 0) {
3387 s->nb_allocated = 1;
3388 s->map[0] = s->map[1] = s->map[2] = s->map[3] = 0;
3390 s->nb_allocated = 2;
3391 s->map[0] = s->map[3] = 0;
3392 s->map[1] = s->map[2] = 1;
3395 for (int i = 0; i < s->nb_allocated; i++)
3396 allocate_plane(s, sizeof_uv, sizeof_ker, i);
3398 calculate_rotation_matrix(s->yaw, s->pitch, s->roll, s->rot_mat, s->rotation_order);
3399 set_mirror_modifier(s->h_flip, s->v_flip, s->d_flip, s->output_mirror_modifier);
3401 ctx->internal->execute(ctx, v360_slice, NULL, NULL, FFMIN(outlink->h, ff_filter_get_nb_threads(ctx)));
3406 static int filter_frame(AVFilterLink *inlink, AVFrame *in)
3408 AVFilterContext *ctx = inlink->dst;
3409 AVFilterLink *outlink = ctx->outputs[0];
3410 V360Context *s = ctx->priv;
3414 out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
3417 return AVERROR(ENOMEM);
3419 av_frame_copy_props(out, in);
3424 ctx->internal->execute(ctx, s->remap_slice, &td, NULL, FFMIN(outlink->h, ff_filter_get_nb_threads(ctx)));
3427 return ff_filter_frame(outlink, out);
3430 static av_cold void uninit(AVFilterContext *ctx)
3432 V360Context *s = ctx->priv;
3434 for (int p = 0; p < s->nb_allocated; p++) {
3437 av_freep(&s->ker[p]);
3441 static const AVFilterPad inputs[] = {
3444 .type = AVMEDIA_TYPE_VIDEO,
3445 .filter_frame = filter_frame,
3450 static const AVFilterPad outputs[] = {
3453 .type = AVMEDIA_TYPE_VIDEO,
3454 .config_props = config_output,
3459 AVFilter ff_vf_v360 = {
3461 .description = NULL_IF_CONFIG_SMALL("Convert 360 projection of video."),
3462 .priv_size = sizeof(V360Context),
3464 .query_formats = query_formats,
3467 .priv_class = &v360_class,
3468 .flags = AVFILTER_FLAG_SLICE_THREADS,