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 { "barrel", "barrel facebook's 360 format", 0, AV_OPT_TYPE_CONST, {.i64=BARREL}, 0, 0, FLAGS, "in" },
65 { "fb", "barrel facebook's 360 format", 0, AV_OPT_TYPE_CONST, {.i64=BARREL}, 0, 0, FLAGS, "in" },
66 { "c1x6", "cubemap 1x6", 0, AV_OPT_TYPE_CONST, {.i64=CUBEMAP_1_6}, 0, 0, FLAGS, "in" },
67 { "sg", "stereographic", 0, AV_OPT_TYPE_CONST, {.i64=STEREOGRAPHIC}, 0, 0, FLAGS, "in" },
68 { "mercator", "mercator", 0, AV_OPT_TYPE_CONST, {.i64=MERCATOR}, 0, 0, FLAGS, "in" },
69 { "ball", "ball", 0, AV_OPT_TYPE_CONST, {.i64=BALL}, 0, 0, FLAGS, "in" },
70 { "hammer", "hammer", 0, AV_OPT_TYPE_CONST, {.i64=HAMMER}, 0, 0, FLAGS, "in" },
71 {"sinusoidal", "sinusoidal", 0, AV_OPT_TYPE_CONST, {.i64=SINUSOIDAL}, 0, 0, FLAGS, "in" },
72 { "output", "set output projection", OFFSET(out), AV_OPT_TYPE_INT, {.i64=CUBEMAP_3_2}, 0, NB_PROJECTIONS-1, FLAGS, "out" },
73 { "e", "equirectangular", 0, AV_OPT_TYPE_CONST, {.i64=EQUIRECTANGULAR}, 0, 0, FLAGS, "out" },
74 { "equirect", "equirectangular", 0, AV_OPT_TYPE_CONST, {.i64=EQUIRECTANGULAR}, 0, 0, FLAGS, "out" },
75 { "c3x2", "cubemap 3x2", 0, AV_OPT_TYPE_CONST, {.i64=CUBEMAP_3_2}, 0, 0, FLAGS, "out" },
76 { "c6x1", "cubemap 6x1", 0, AV_OPT_TYPE_CONST, {.i64=CUBEMAP_6_1}, 0, 0, FLAGS, "out" },
77 { "eac", "equi-angular cubemap", 0, AV_OPT_TYPE_CONST, {.i64=EQUIANGULAR}, 0, 0, FLAGS, "out" },
78 { "dfisheye", "dual fisheye", 0, AV_OPT_TYPE_CONST, {.i64=DUAL_FISHEYE}, 0, 0, FLAGS, "out" },
79 { "flat", "regular video", 0, AV_OPT_TYPE_CONST, {.i64=FLAT}, 0, 0, FLAGS, "out" },
80 {"rectilinear", "regular video", 0, AV_OPT_TYPE_CONST, {.i64=FLAT}, 0, 0, FLAGS, "out" },
81 { "gnomonic", "regular video", 0, AV_OPT_TYPE_CONST, {.i64=FLAT}, 0, 0, FLAGS, "out" },
82 { "barrel", "barrel facebook's 360 format", 0, AV_OPT_TYPE_CONST, {.i64=BARREL}, 0, 0, FLAGS, "out" },
83 { "fb", "barrel facebook's 360 format", 0, AV_OPT_TYPE_CONST, {.i64=BARREL}, 0, 0, FLAGS, "out" },
84 { "c1x6", "cubemap 1x6", 0, AV_OPT_TYPE_CONST, {.i64=CUBEMAP_1_6}, 0, 0, FLAGS, "out" },
85 { "sg", "stereographic", 0, AV_OPT_TYPE_CONST, {.i64=STEREOGRAPHIC}, 0, 0, FLAGS, "out" },
86 { "mercator", "mercator", 0, AV_OPT_TYPE_CONST, {.i64=MERCATOR}, 0, 0, FLAGS, "out" },
87 { "ball", "ball", 0, AV_OPT_TYPE_CONST, {.i64=BALL}, 0, 0, FLAGS, "out" },
88 { "hammer", "hammer", 0, AV_OPT_TYPE_CONST, {.i64=HAMMER}, 0, 0, FLAGS, "out" },
89 {"sinusoidal", "sinusoidal", 0, AV_OPT_TYPE_CONST, {.i64=SINUSOIDAL}, 0, 0, FLAGS, "out" },
90 { "fisheye", "fisheye", 0, AV_OPT_TYPE_CONST, {.i64=FISHEYE}, 0, 0, FLAGS, "out" },
91 { "pannini", "pannini", 0, AV_OPT_TYPE_CONST, {.i64=PANNINI}, 0, 0, FLAGS, "out" },
92 {"cylindrical", "cylindrical", 0, AV_OPT_TYPE_CONST, {.i64=CYLINDRICAL}, 0, 0, FLAGS, "out" },
93 { "interp", "set interpolation method", OFFSET(interp), AV_OPT_TYPE_INT, {.i64=BILINEAR}, 0, NB_INTERP_METHODS-1, FLAGS, "interp" },
94 { "near", "nearest neighbour", 0, AV_OPT_TYPE_CONST, {.i64=NEAREST}, 0, 0, FLAGS, "interp" },
95 { "nearest", "nearest neighbour", 0, AV_OPT_TYPE_CONST, {.i64=NEAREST}, 0, 0, FLAGS, "interp" },
96 { "line", "bilinear interpolation", 0, AV_OPT_TYPE_CONST, {.i64=BILINEAR}, 0, 0, FLAGS, "interp" },
97 { "linear", "bilinear interpolation", 0, AV_OPT_TYPE_CONST, {.i64=BILINEAR}, 0, 0, FLAGS, "interp" },
98 { "cube", "bicubic interpolation", 0, AV_OPT_TYPE_CONST, {.i64=BICUBIC}, 0, 0, FLAGS, "interp" },
99 { "cubic", "bicubic interpolation", 0, AV_OPT_TYPE_CONST, {.i64=BICUBIC}, 0, 0, FLAGS, "interp" },
100 { "lanc", "lanczos interpolation", 0, AV_OPT_TYPE_CONST, {.i64=LANCZOS}, 0, 0, FLAGS, "interp" },
101 { "lanczos", "lanczos interpolation", 0, AV_OPT_TYPE_CONST, {.i64=LANCZOS}, 0, 0, FLAGS, "interp" },
102 { "w", "output width", OFFSET(width), AV_OPT_TYPE_INT, {.i64=0}, 0, INT16_MAX, FLAGS, "w"},
103 { "h", "output height", OFFSET(height), AV_OPT_TYPE_INT, {.i64=0}, 0, INT16_MAX, FLAGS, "h"},
104 { "in_stereo", "input stereo format", OFFSET(in_stereo), AV_OPT_TYPE_INT, {.i64=STEREO_2D}, 0, NB_STEREO_FMTS-1, FLAGS, "stereo" },
105 {"out_stereo", "output stereo format", OFFSET(out_stereo), AV_OPT_TYPE_INT, {.i64=STEREO_2D}, 0, NB_STEREO_FMTS-1, FLAGS, "stereo" },
106 { "2d", "2d mono", 0, AV_OPT_TYPE_CONST, {.i64=STEREO_2D}, 0, 0, FLAGS, "stereo" },
107 { "sbs", "side by side", 0, AV_OPT_TYPE_CONST, {.i64=STEREO_SBS}, 0, 0, FLAGS, "stereo" },
108 { "tb", "top bottom", 0, AV_OPT_TYPE_CONST, {.i64=STEREO_TB}, 0, 0, FLAGS, "stereo" },
109 { "in_forder", "input cubemap face order", OFFSET(in_forder), AV_OPT_TYPE_STRING, {.str="rludfb"}, 0, NB_DIRECTIONS-1, FLAGS, "in_forder"},
110 {"out_forder", "output cubemap face order", OFFSET(out_forder), AV_OPT_TYPE_STRING, {.str="rludfb"}, 0, NB_DIRECTIONS-1, FLAGS, "out_forder"},
111 { "in_frot", "input cubemap face rotation", OFFSET(in_frot), AV_OPT_TYPE_STRING, {.str="000000"}, 0, NB_DIRECTIONS-1, FLAGS, "in_frot"},
112 { "out_frot", "output cubemap face rotation",OFFSET(out_frot), AV_OPT_TYPE_STRING, {.str="000000"}, 0, NB_DIRECTIONS-1, FLAGS, "out_frot"},
113 { "in_pad", "percent input cubemap pads", OFFSET(in_pad), AV_OPT_TYPE_FLOAT, {.dbl=0.f}, 0.f, 1.f, FLAGS, "in_pad"},
114 { "out_pad", "percent output cubemap pads", OFFSET(out_pad), AV_OPT_TYPE_FLOAT, {.dbl=0.f}, 0.f, 1.f, FLAGS, "out_pad"},
115 { "fin_pad", "fixed input cubemap pads", OFFSET(fin_pad), AV_OPT_TYPE_INT, {.i64=0}, 0, 100, FLAGS, "fin_pad"},
116 { "fout_pad", "fixed output cubemap pads", OFFSET(fout_pad), AV_OPT_TYPE_INT, {.i64=0}, 0, 100, FLAGS, "fout_pad"},
117 { "yaw", "yaw rotation", OFFSET(yaw), AV_OPT_TYPE_FLOAT, {.dbl=0.f}, -180.f, 180.f, FLAGS, "yaw"},
118 { "pitch", "pitch rotation", OFFSET(pitch), AV_OPT_TYPE_FLOAT, {.dbl=0.f}, -180.f, 180.f, FLAGS, "pitch"},
119 { "roll", "roll rotation", OFFSET(roll), AV_OPT_TYPE_FLOAT, {.dbl=0.f}, -180.f, 180.f, FLAGS, "roll"},
120 { "rorder", "rotation order", OFFSET(rorder), AV_OPT_TYPE_STRING, {.str="ypr"}, 0, 0, FLAGS, "rorder"},
121 { "h_fov", "horizontal field of view", OFFSET(h_fov), AV_OPT_TYPE_FLOAT, {.dbl=90.f}, 0.00001f, 360.f, FLAGS, "h_fov"},
122 { "v_fov", "vertical field of view", OFFSET(v_fov), AV_OPT_TYPE_FLOAT, {.dbl=45.f}, 0.00001f, 360.f, FLAGS, "v_fov"},
123 { "d_fov", "diagonal field of view", OFFSET(d_fov), AV_OPT_TYPE_FLOAT, {.dbl=0.f}, 0.f, 360.f, FLAGS, "d_fov"},
124 { "h_flip", "flip out video horizontally", OFFSET(h_flip), AV_OPT_TYPE_BOOL, {.i64=0}, 0, 1, FLAGS, "h_flip"},
125 { "v_flip", "flip out video vertically", OFFSET(v_flip), AV_OPT_TYPE_BOOL, {.i64=0}, 0, 1, FLAGS, "v_flip"},
126 { "d_flip", "flip out video indepth", OFFSET(d_flip), AV_OPT_TYPE_BOOL, {.i64=0}, 0, 1, FLAGS, "d_flip"},
127 { "ih_flip", "flip in video horizontally", OFFSET(ih_flip), AV_OPT_TYPE_BOOL, {.i64=0}, 0, 1, FLAGS, "ih_flip"},
128 { "iv_flip", "flip in video vertically", OFFSET(iv_flip), AV_OPT_TYPE_BOOL, {.i64=0}, 0, 1, FLAGS, "iv_flip"},
129 { "in_trans", "transpose video input", OFFSET(in_transpose), AV_OPT_TYPE_BOOL, {.i64=0}, 0, 1, FLAGS, "in_transpose"},
130 { "out_trans", "transpose video output", OFFSET(out_transpose), AV_OPT_TYPE_BOOL, {.i64=0}, 0, 1, FLAGS, "out_transpose"},
134 AVFILTER_DEFINE_CLASS(v360);
136 static int query_formats(AVFilterContext *ctx)
138 static const enum AVPixelFormat pix_fmts[] = {
140 AV_PIX_FMT_YUVA444P, AV_PIX_FMT_YUVA444P9,
141 AV_PIX_FMT_YUVA444P10, AV_PIX_FMT_YUVA444P12,
142 AV_PIX_FMT_YUVA444P16,
145 AV_PIX_FMT_YUVA422P, AV_PIX_FMT_YUVA422P9,
146 AV_PIX_FMT_YUVA422P10, AV_PIX_FMT_YUVA422P12,
147 AV_PIX_FMT_YUVA422P16,
150 AV_PIX_FMT_YUVA420P, AV_PIX_FMT_YUVA420P9,
151 AV_PIX_FMT_YUVA420P10, AV_PIX_FMT_YUVA420P16,
154 AV_PIX_FMT_YUVJ444P, AV_PIX_FMT_YUVJ440P,
155 AV_PIX_FMT_YUVJ422P, AV_PIX_FMT_YUVJ420P,
159 AV_PIX_FMT_YUV444P, AV_PIX_FMT_YUV444P9,
160 AV_PIX_FMT_YUV444P10, AV_PIX_FMT_YUV444P12,
161 AV_PIX_FMT_YUV444P14, AV_PIX_FMT_YUV444P16,
164 AV_PIX_FMT_YUV440P, AV_PIX_FMT_YUV440P10,
165 AV_PIX_FMT_YUV440P12,
168 AV_PIX_FMT_YUV422P, AV_PIX_FMT_YUV422P9,
169 AV_PIX_FMT_YUV422P10, AV_PIX_FMT_YUV422P12,
170 AV_PIX_FMT_YUV422P14, AV_PIX_FMT_YUV422P16,
173 AV_PIX_FMT_YUV420P, AV_PIX_FMT_YUV420P9,
174 AV_PIX_FMT_YUV420P10, AV_PIX_FMT_YUV420P12,
175 AV_PIX_FMT_YUV420P14, AV_PIX_FMT_YUV420P16,
184 AV_PIX_FMT_GBRP, AV_PIX_FMT_GBRP9,
185 AV_PIX_FMT_GBRP10, AV_PIX_FMT_GBRP12,
186 AV_PIX_FMT_GBRP14, AV_PIX_FMT_GBRP16,
189 AV_PIX_FMT_GBRAP, AV_PIX_FMT_GBRAP10,
190 AV_PIX_FMT_GBRAP12, AV_PIX_FMT_GBRAP16,
193 AV_PIX_FMT_GRAY8, AV_PIX_FMT_GRAY9,
194 AV_PIX_FMT_GRAY10, AV_PIX_FMT_GRAY12,
195 AV_PIX_FMT_GRAY14, AV_PIX_FMT_GRAY16,
200 AVFilterFormats *fmts_list = ff_make_format_list(pix_fmts);
202 return AVERROR(ENOMEM);
203 return ff_set_common_formats(ctx, fmts_list);
206 #define DEFINE_REMAP1_LINE(bits, div) \
207 static void remap1_##bits##bit_line_c(uint8_t *dst, int width, const uint8_t *src, \
208 ptrdiff_t in_linesize, \
209 const uint16_t *u, const uint16_t *v, const int16_t *ker) \
211 const uint##bits##_t *s = (const uint##bits##_t *)src; \
212 uint##bits##_t *d = (uint##bits##_t *)dst; \
214 in_linesize /= div; \
216 for (int x = 0; x < width; x++) \
217 d[x] = s[v[x] * in_linesize + u[x]]; \
220 DEFINE_REMAP1_LINE( 8, 1)
221 DEFINE_REMAP1_LINE(16, 2)
224 * Generate remapping function with a given window size and pixel depth.
226 * @param ws size of interpolation window
227 * @param bits number of bits per pixel
229 #define DEFINE_REMAP(ws, bits) \
230 static int remap##ws##_##bits##bit_slice(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs) \
232 ThreadData *td = arg; \
233 const V360Context *s = ctx->priv; \
234 const AVFrame *in = td->in; \
235 AVFrame *out = td->out; \
237 for (int stereo = 0; stereo < 1 + s->out_stereo > STEREO_2D; stereo++) { \
238 for (int plane = 0; plane < s->nb_planes; plane++) { \
239 const int in_linesize = in->linesize[plane]; \
240 const int out_linesize = out->linesize[plane]; \
241 const int uv_linesize = s->uv_linesize[plane]; \
242 const int in_offset_w = stereo ? s->in_offset_w[plane] : 0; \
243 const int in_offset_h = stereo ? s->in_offset_h[plane] : 0; \
244 const int out_offset_w = stereo ? s->out_offset_w[plane] : 0; \
245 const int out_offset_h = stereo ? s->out_offset_h[plane] : 0; \
246 const uint8_t *src = in->data[plane] + in_offset_h * in_linesize + in_offset_w * (bits >> 3); \
247 uint8_t *dst = out->data[plane] + out_offset_h * out_linesize + out_offset_w * (bits >> 3); \
248 const int width = s->pr_width[plane]; \
249 const int height = s->pr_height[plane]; \
251 const int slice_start = (height * jobnr ) / nb_jobs; \
252 const int slice_end = (height * (jobnr + 1)) / nb_jobs; \
254 for (int y = slice_start; y < slice_end; y++) { \
255 const unsigned map = s->map[plane]; \
256 const uint16_t *u = s->u[map] + y * uv_linesize * ws * ws; \
257 const uint16_t *v = s->v[map] + y * uv_linesize * ws * ws; \
258 const int16_t *ker = s->ker[map] + y * uv_linesize * ws * ws; \
260 s->remap_line(dst + y * out_linesize, width, src, in_linesize, u, v, ker); \
275 #define DEFINE_REMAP_LINE(ws, bits, div) \
276 static void remap##ws##_##bits##bit_line_c(uint8_t *dst, int width, const uint8_t *src, \
277 ptrdiff_t in_linesize, \
278 const uint16_t *u, const uint16_t *v, const int16_t *ker) \
280 const uint##bits##_t *s = (const uint##bits##_t *)src; \
281 uint##bits##_t *d = (uint##bits##_t *)dst; \
283 in_linesize /= div; \
285 for (int x = 0; x < width; x++) { \
286 const uint16_t *uu = u + x * ws * ws; \
287 const uint16_t *vv = v + x * ws * ws; \
288 const int16_t *kker = ker + x * ws * ws; \
291 for (int i = 0; i < ws; i++) { \
292 for (int j = 0; j < ws; j++) { \
293 tmp += kker[i * ws + j] * s[vv[i * ws + j] * in_linesize + uu[i * ws + j]]; \
297 d[x] = av_clip_uint##bits(tmp >> 14); \
301 DEFINE_REMAP_LINE(2, 8, 1)
302 DEFINE_REMAP_LINE(4, 8, 1)
303 DEFINE_REMAP_LINE(2, 16, 2)
304 DEFINE_REMAP_LINE(4, 16, 2)
306 void ff_v360_init(V360Context *s, int depth)
310 s->remap_line = depth <= 8 ? remap1_8bit_line_c : remap1_16bit_line_c;
313 s->remap_line = depth <= 8 ? remap2_8bit_line_c : remap2_16bit_line_c;
317 s->remap_line = depth <= 8 ? remap4_8bit_line_c : remap4_16bit_line_c;
322 ff_v360_init_x86(s, depth);
326 * Save nearest pixel coordinates for remapping.
328 * @param du horizontal relative coordinate
329 * @param dv vertical relative coordinate
330 * @param rmap calculated 4x4 window
331 * @param u u remap data
332 * @param v v remap data
333 * @param ker ker remap data
335 static void nearest_kernel(float du, float dv, const XYRemap *rmap,
336 uint16_t *u, uint16_t *v, int16_t *ker)
338 const int i = roundf(dv) + 1;
339 const int j = roundf(du) + 1;
341 u[0] = rmap->u[i][j];
342 v[0] = rmap->v[i][j];
346 * Calculate kernel for bilinear interpolation.
348 * @param du horizontal relative coordinate
349 * @param dv vertical relative coordinate
350 * @param rmap calculated 4x4 window
351 * @param u u remap data
352 * @param v v remap data
353 * @param ker ker remap data
355 static void bilinear_kernel(float du, float dv, const XYRemap *rmap,
356 uint16_t *u, uint16_t *v, int16_t *ker)
358 for (int i = 0; i < 2; i++) {
359 for (int j = 0; j < 2; j++) {
360 u[i * 2 + j] = rmap->u[i + 1][j + 1];
361 v[i * 2 + j] = rmap->v[i + 1][j + 1];
365 ker[0] = lrintf((1.f - du) * (1.f - dv) * 16385.f);
366 ker[1] = lrintf( du * (1.f - dv) * 16385.f);
367 ker[2] = lrintf((1.f - du) * dv * 16385.f);
368 ker[3] = lrintf( du * dv * 16385.f);
372 * Calculate 1-dimensional cubic coefficients.
374 * @param t relative coordinate
375 * @param coeffs coefficients
377 static inline void calculate_bicubic_coeffs(float t, float *coeffs)
379 const float tt = t * t;
380 const float ttt = t * t * t;
382 coeffs[0] = - t / 3.f + tt / 2.f - ttt / 6.f;
383 coeffs[1] = 1.f - t / 2.f - tt + ttt / 2.f;
384 coeffs[2] = t + tt / 2.f - ttt / 2.f;
385 coeffs[3] = - t / 6.f + ttt / 6.f;
389 * Calculate kernel for bicubic interpolation.
391 * @param du horizontal relative coordinate
392 * @param dv vertical relative coordinate
393 * @param rmap calculated 4x4 window
394 * @param u u remap data
395 * @param v v remap data
396 * @param ker ker remap data
398 static void bicubic_kernel(float du, float dv, const XYRemap *rmap,
399 uint16_t *u, uint16_t *v, int16_t *ker)
404 calculate_bicubic_coeffs(du, du_coeffs);
405 calculate_bicubic_coeffs(dv, dv_coeffs);
407 for (int i = 0; i < 4; i++) {
408 for (int j = 0; j < 4; j++) {
409 u[i * 4 + j] = rmap->u[i][j];
410 v[i * 4 + j] = rmap->v[i][j];
411 ker[i * 4 + j] = lrintf(du_coeffs[j] * dv_coeffs[i] * 16385.f);
417 * Calculate 1-dimensional lanczos coefficients.
419 * @param t relative coordinate
420 * @param coeffs coefficients
422 static inline void calculate_lanczos_coeffs(float t, float *coeffs)
426 for (int i = 0; i < 4; i++) {
427 const float x = M_PI * (t - i + 1);
431 coeffs[i] = sinf(x) * sinf(x / 2.f) / (x * x / 2.f);
436 for (int i = 0; i < 4; i++) {
442 * Calculate kernel for lanczos interpolation.
444 * @param du horizontal relative coordinate
445 * @param dv vertical relative coordinate
446 * @param rmap calculated 4x4 window
447 * @param u u remap data
448 * @param v v remap data
449 * @param ker ker remap data
451 static void lanczos_kernel(float du, float dv, const XYRemap *rmap,
452 uint16_t *u, uint16_t *v, int16_t *ker)
457 calculate_lanczos_coeffs(du, du_coeffs);
458 calculate_lanczos_coeffs(dv, dv_coeffs);
460 for (int i = 0; i < 4; i++) {
461 for (int j = 0; j < 4; j++) {
462 u[i * 4 + j] = rmap->u[i][j];
463 v[i * 4 + j] = rmap->v[i][j];
464 ker[i * 4 + j] = lrintf(du_coeffs[j] * dv_coeffs[i] * 16385.f);
470 * Modulo operation with only positive remainders.
475 * @return positive remainder of (a / b)
477 static inline int mod(int a, int b)
479 const int res = a % b;
488 * Convert char to corresponding direction.
489 * Used for cubemap options.
491 static int get_direction(char c)
512 * Convert char to corresponding rotation angle.
513 * Used for cubemap options.
515 static int get_rotation(char c)
532 * Convert char to corresponding rotation order.
534 static int get_rorder(char c)
552 * Prepare data for processing cubemap input format.
554 * @param ctx filter context
558 static int prepare_cube_in(AVFilterContext *ctx)
560 V360Context *s = ctx->priv;
562 for (int face = 0; face < NB_FACES; face++) {
563 const char c = s->in_forder[face];
567 av_log(ctx, AV_LOG_ERROR,
568 "Incomplete in_forder option. Direction for all 6 faces should be specified.\n");
569 return AVERROR(EINVAL);
572 direction = get_direction(c);
573 if (direction == -1) {
574 av_log(ctx, AV_LOG_ERROR,
575 "Incorrect direction symbol '%c' in in_forder option.\n", c);
576 return AVERROR(EINVAL);
579 s->in_cubemap_face_order[direction] = face;
582 for (int face = 0; face < NB_FACES; face++) {
583 const char c = s->in_frot[face];
587 av_log(ctx, AV_LOG_ERROR,
588 "Incomplete in_frot option. Rotation for all 6 faces should be specified.\n");
589 return AVERROR(EINVAL);
592 rotation = get_rotation(c);
593 if (rotation == -1) {
594 av_log(ctx, AV_LOG_ERROR,
595 "Incorrect rotation symbol '%c' in in_frot option.\n", c);
596 return AVERROR(EINVAL);
599 s->in_cubemap_face_rotation[face] = rotation;
606 * Prepare data for processing cubemap output format.
608 * @param ctx filter context
612 static int prepare_cube_out(AVFilterContext *ctx)
614 V360Context *s = ctx->priv;
616 for (int face = 0; face < NB_FACES; face++) {
617 const char c = s->out_forder[face];
621 av_log(ctx, AV_LOG_ERROR,
622 "Incomplete out_forder option. Direction for all 6 faces should be specified.\n");
623 return AVERROR(EINVAL);
626 direction = get_direction(c);
627 if (direction == -1) {
628 av_log(ctx, AV_LOG_ERROR,
629 "Incorrect direction symbol '%c' in out_forder option.\n", c);
630 return AVERROR(EINVAL);
633 s->out_cubemap_direction_order[face] = direction;
636 for (int face = 0; face < NB_FACES; face++) {
637 const char c = s->out_frot[face];
641 av_log(ctx, AV_LOG_ERROR,
642 "Incomplete out_frot option. Rotation for all 6 faces should be specified.\n");
643 return AVERROR(EINVAL);
646 rotation = get_rotation(c);
647 if (rotation == -1) {
648 av_log(ctx, AV_LOG_ERROR,
649 "Incorrect rotation symbol '%c' in out_frot option.\n", c);
650 return AVERROR(EINVAL);
653 s->out_cubemap_face_rotation[face] = rotation;
659 static inline void rotate_cube_face(float *uf, float *vf, int rotation)
685 static inline void rotate_cube_face_inverse(float *uf, float *vf, int rotation)
716 static void normalize_vector(float *vec)
718 const float norm = sqrtf(vec[0] * vec[0] + vec[1] * vec[1] + vec[2] * vec[2]);
726 * Calculate 3D coordinates on sphere for corresponding cubemap position.
727 * Common operation for every cubemap.
729 * @param s filter private context
730 * @param uf horizontal cubemap coordinate [0, 1)
731 * @param vf vertical cubemap coordinate [0, 1)
732 * @param face face of cubemap
733 * @param vec coordinates on sphere
734 * @param scalew scale for uf
735 * @param scaleh scale for vf
737 static void cube_to_xyz(const V360Context *s,
738 float uf, float vf, int face,
739 float *vec, float scalew, float scaleh)
741 const int direction = s->out_cubemap_direction_order[face];
747 rotate_cube_face_inverse(&uf, &vf, s->out_cubemap_face_rotation[face]);
788 normalize_vector(vec);
792 * Calculate cubemap position for corresponding 3D coordinates on sphere.
793 * Common operation for every cubemap.
795 * @param s filter private context
796 * @param vec coordinated on sphere
797 * @param uf horizontal cubemap coordinate [0, 1)
798 * @param vf vertical cubemap coordinate [0, 1)
799 * @param direction direction of view
801 static void xyz_to_cube(const V360Context *s,
803 float *uf, float *vf, int *direction)
805 const float phi = atan2f(vec[0], -vec[2]);
806 const float theta = asinf(-vec[1]);
807 float phi_norm, theta_threshold;
810 if (phi >= -M_PI_4 && phi < M_PI_4) {
813 } else if (phi >= -(M_PI_2 + M_PI_4) && phi < -M_PI_4) {
815 phi_norm = phi + M_PI_2;
816 } else if (phi >= M_PI_4 && phi < M_PI_2 + M_PI_4) {
818 phi_norm = phi - M_PI_2;
821 phi_norm = phi + ((phi > 0.f) ? -M_PI : M_PI);
824 theta_threshold = atanf(cosf(phi_norm));
825 if (theta > theta_threshold) {
827 } else if (theta < -theta_threshold) {
831 switch (*direction) {
833 *uf = vec[2] / vec[0];
834 *vf = -vec[1] / vec[0];
837 *uf = vec[2] / vec[0];
838 *vf = vec[1] / vec[0];
841 *uf = vec[0] / vec[1];
842 *vf = -vec[2] / vec[1];
845 *uf = -vec[0] / vec[1];
846 *vf = -vec[2] / vec[1];
849 *uf = -vec[0] / vec[2];
850 *vf = vec[1] / vec[2];
853 *uf = -vec[0] / vec[2];
854 *vf = -vec[1] / vec[2];
860 face = s->in_cubemap_face_order[*direction];
861 rotate_cube_face(uf, vf, s->in_cubemap_face_rotation[face]);
863 (*uf) *= s->input_mirror_modifier[0];
864 (*vf) *= s->input_mirror_modifier[1];
868 * Find position on another cube face in case of overflow/underflow.
869 * Used for calculation of interpolation window.
871 * @param s filter private context
872 * @param uf horizontal cubemap coordinate
873 * @param vf vertical cubemap coordinate
874 * @param direction direction of view
875 * @param new_uf new horizontal cubemap coordinate
876 * @param new_vf new vertical cubemap coordinate
877 * @param face face position on cubemap
879 static void process_cube_coordinates(const V360Context *s,
880 float uf, float vf, int direction,
881 float *new_uf, float *new_vf, int *face)
884 * Cubemap orientation
891 * +-------+-------+-------+-------+ ^ e |
893 * | left | front | right | back | | g |
894 * +-------+-------+-------+-------+ v h v
900 *face = s->in_cubemap_face_order[direction];
901 rotate_cube_face_inverse(&uf, &vf, s->in_cubemap_face_rotation[*face]);
903 if ((uf < -1.f || uf >= 1.f) && (vf < -1.f || vf >= 1.f)) {
904 // There are no pixels to use in this case
907 } else if (uf < -1.f) {
943 } else if (uf >= 1.f) {
979 } else if (vf < -1.f) {
1015 } else if (vf >= 1.f) {
1017 switch (direction) {
1057 *face = s->in_cubemap_face_order[direction];
1058 rotate_cube_face(new_uf, new_vf, s->in_cubemap_face_rotation[*face]);
1062 * Calculate 3D coordinates on sphere for corresponding frame position in cubemap3x2 format.
1064 * @param s filter private context
1065 * @param i horizontal position on frame [0, width)
1066 * @param j vertical position on frame [0, height)
1067 * @param width frame width
1068 * @param height frame height
1069 * @param vec coordinates on sphere
1071 static void cube3x2_to_xyz(const V360Context *s,
1072 int i, int j, int width, int height,
1075 const float scalew = s->fout_pad > 0 ? 1.f - s->fout_pad / (s->out_width / 3.f) : 1.f - s->out_pad;
1076 const float scaleh = s->fout_pad > 0 ? 1.f - s->fout_pad / (s->out_height / 2.f) : 1.f - s->out_pad;
1078 const float ew = width / 3.f;
1079 const float eh = height / 2.f;
1081 const int u_face = floorf(i / ew);
1082 const int v_face = floorf(j / eh);
1083 const int face = u_face + 3 * v_face;
1085 const int u_shift = ceilf(ew * u_face);
1086 const int v_shift = ceilf(eh * v_face);
1087 const int ewi = ceilf(ew * (u_face + 1)) - u_shift;
1088 const int ehi = ceilf(eh * (v_face + 1)) - v_shift;
1090 const float uf = 2.f * (i - u_shift + 0.5f) / ewi - 1.f;
1091 const float vf = 2.f * (j - v_shift + 0.5f) / ehi - 1.f;
1093 cube_to_xyz(s, uf, vf, face, vec, scalew, scaleh);
1097 * Calculate frame position in cubemap3x2 format for corresponding 3D coordinates on sphere.
1099 * @param s filter private context
1100 * @param vec coordinates on sphere
1101 * @param width frame width
1102 * @param height frame height
1103 * @param us horizontal coordinates for interpolation window
1104 * @param vs vertical coordinates for interpolation window
1105 * @param du horizontal relative coordinate
1106 * @param dv vertical relative coordinate
1108 static void xyz_to_cube3x2(const V360Context *s,
1109 const float *vec, int width, int height,
1110 uint16_t us[4][4], uint16_t vs[4][4], float *du, float *dv)
1112 const float scalew = s->fin_pad > 0 ? 1.f - s->fin_pad / (s->in_width / 3.f) : 1.f - s->in_pad;
1113 const float scaleh = s->fin_pad > 0 ? 1.f - s->fin_pad / (s->in_height / 2.f) : 1.f - s->in_pad;
1114 const float ew = width / 3.f;
1115 const float eh = height / 2.f;
1119 int direction, face;
1122 xyz_to_cube(s, vec, &uf, &vf, &direction);
1127 face = s->in_cubemap_face_order[direction];
1130 ewi = ceilf(ew * (u_face + 1)) - ceilf(ew * u_face);
1131 ehi = ceilf(eh * (v_face + 1)) - ceilf(eh * v_face);
1133 uf = 0.5f * ewi * (uf + 1.f) - 0.5f;
1134 vf = 0.5f * ehi * (vf + 1.f) - 0.5f;
1142 for (int i = -1; i < 3; i++) {
1143 for (int j = -1; j < 3; j++) {
1144 int new_ui = ui + j;
1145 int new_vi = vi + i;
1146 int u_shift, v_shift;
1147 int new_ewi, new_ehi;
1149 if (new_ui >= 0 && new_ui < ewi && new_vi >= 0 && new_vi < ehi) {
1150 face = s->in_cubemap_face_order[direction];
1154 u_shift = ceilf(ew * u_face);
1155 v_shift = ceilf(eh * v_face);
1157 uf = 2.f * new_ui / ewi - 1.f;
1158 vf = 2.f * new_vi / ehi - 1.f;
1163 process_cube_coordinates(s, uf, vf, direction, &uf, &vf, &face);
1170 u_shift = ceilf(ew * u_face);
1171 v_shift = ceilf(eh * v_face);
1172 new_ewi = ceilf(ew * (u_face + 1)) - u_shift;
1173 new_ehi = ceilf(eh * (v_face + 1)) - v_shift;
1175 new_ui = av_clip(roundf(0.5f * new_ewi * (uf + 1.f)), 0, new_ewi - 1);
1176 new_vi = av_clip(roundf(0.5f * new_ehi * (vf + 1.f)), 0, new_ehi - 1);
1179 us[i + 1][j + 1] = u_shift + new_ui;
1180 vs[i + 1][j + 1] = v_shift + new_vi;
1186 * Calculate 3D coordinates on sphere for corresponding frame position in cubemap1x6 format.
1188 * @param s filter private context
1189 * @param i horizontal position on frame [0, width)
1190 * @param j vertical position on frame [0, height)
1191 * @param width frame width
1192 * @param height frame height
1193 * @param vec coordinates on sphere
1195 static void cube1x6_to_xyz(const V360Context *s,
1196 int i, int j, int width, int height,
1199 const float scalew = s->fout_pad > 0 ? 1.f - (float)(s->fout_pad) / s->out_width : 1.f - s->out_pad;
1200 const float scaleh = s->fout_pad > 0 ? 1.f - s->fout_pad / (s->out_height / 6.f) : 1.f - s->out_pad;
1202 const float ew = width;
1203 const float eh = height / 6.f;
1205 const int face = floorf(j / eh);
1207 const int v_shift = ceilf(eh * face);
1208 const int ehi = ceilf(eh * (face + 1)) - v_shift;
1210 const float uf = 2.f * (i + 0.5f) / ew - 1.f;
1211 const float vf = 2.f * (j - v_shift + 0.5f) / ehi - 1.f;
1213 cube_to_xyz(s, uf, vf, face, vec, scalew, scaleh);
1217 * Calculate 3D coordinates on sphere for corresponding frame position in cubemap6x1 format.
1219 * @param s filter private context
1220 * @param i horizontal position on frame [0, width)
1221 * @param j vertical position on frame [0, height)
1222 * @param width frame width
1223 * @param height frame height
1224 * @param vec coordinates on sphere
1226 static void cube6x1_to_xyz(const V360Context *s,
1227 int i, int j, int width, int height,
1230 const float scalew = s->fout_pad > 0 ? 1.f - s->fout_pad / (s->out_width / 6.f) : 1.f - s->out_pad;
1231 const float scaleh = s->fout_pad > 0 ? 1.f - (float)(s->fout_pad) / s->out_height : 1.f - s->out_pad;
1233 const float ew = width / 6.f;
1234 const float eh = height;
1236 const int face = floorf(i / ew);
1238 const int u_shift = ceilf(ew * face);
1239 const int ewi = ceilf(ew * (face + 1)) - u_shift;
1241 const float uf = 2.f * (i - u_shift + 0.5f) / ewi - 1.f;
1242 const float vf = 2.f * (j + 0.5f) / eh - 1.f;
1244 cube_to_xyz(s, uf, vf, face, vec, scalew, scaleh);
1248 * Calculate frame position in cubemap1x6 format for corresponding 3D coordinates on sphere.
1250 * @param s filter private context
1251 * @param vec coordinates on sphere
1252 * @param width frame width
1253 * @param height frame height
1254 * @param us horizontal coordinates for interpolation window
1255 * @param vs vertical coordinates for interpolation window
1256 * @param du horizontal relative coordinate
1257 * @param dv vertical relative coordinate
1259 static void xyz_to_cube1x6(const V360Context *s,
1260 const float *vec, int width, int height,
1261 uint16_t us[4][4], uint16_t vs[4][4], float *du, float *dv)
1263 const float scalew = s->fin_pad > 0 ? 1.f - (float)(s->fin_pad) / s->in_width : 1.f - s->in_pad;
1264 const float scaleh = s->fin_pad > 0 ? 1.f - s->fin_pad / (s->in_height / 6.f) : 1.f - s->in_pad;
1265 const float eh = height / 6.f;
1266 const int ewi = width;
1270 int direction, face;
1272 xyz_to_cube(s, vec, &uf, &vf, &direction);
1277 face = s->in_cubemap_face_order[direction];
1278 ehi = ceilf(eh * (face + 1)) - ceilf(eh * face);
1280 uf = 0.5f * ewi * (uf + 1.f) - 0.5f;
1281 vf = 0.5f * ehi * (vf + 1.f) - 0.5f;
1289 for (int i = -1; i < 3; i++) {
1290 for (int j = -1; j < 3; j++) {
1291 int new_ui = ui + j;
1292 int new_vi = vi + i;
1296 if (new_ui >= 0 && new_ui < ewi && new_vi >= 0 && new_vi < ehi) {
1297 face = s->in_cubemap_face_order[direction];
1299 v_shift = ceilf(eh * face);
1301 uf = 2.f * new_ui / ewi - 1.f;
1302 vf = 2.f * new_vi / ehi - 1.f;
1307 process_cube_coordinates(s, uf, vf, direction, &uf, &vf, &face);
1312 v_shift = ceilf(eh * face);
1313 new_ehi = ceilf(eh * (face + 1)) - v_shift;
1315 new_ui = av_clip(roundf(0.5f * ewi * (uf + 1.f)), 0, ewi - 1);
1316 new_vi = av_clip(roundf(0.5f * new_ehi * (vf + 1.f)), 0, new_ehi - 1);
1319 us[i + 1][j + 1] = new_ui;
1320 vs[i + 1][j + 1] = v_shift + new_vi;
1326 * Calculate frame position in cubemap6x1 format for corresponding 3D coordinates on sphere.
1328 * @param s filter private context
1329 * @param vec coordinates on sphere
1330 * @param width frame width
1331 * @param height frame height
1332 * @param us horizontal coordinates for interpolation window
1333 * @param vs vertical coordinates for interpolation window
1334 * @param du horizontal relative coordinate
1335 * @param dv vertical relative coordinate
1337 static void xyz_to_cube6x1(const V360Context *s,
1338 const float *vec, int width, int height,
1339 uint16_t us[4][4], uint16_t vs[4][4], float *du, float *dv)
1341 const float scalew = s->fin_pad > 0 ? 1.f - s->fin_pad / (s->in_width / 6.f) : 1.f - s->in_pad;
1342 const float scaleh = s->fin_pad > 0 ? 1.f - (float)(s->fin_pad) / s->in_height : 1.f - s->in_pad;
1343 const float ew = width / 6.f;
1344 const int ehi = height;
1348 int direction, face;
1350 xyz_to_cube(s, vec, &uf, &vf, &direction);
1355 face = s->in_cubemap_face_order[direction];
1356 ewi = ceilf(ew * (face + 1)) - ceilf(ew * face);
1358 uf = 0.5f * ewi * (uf + 1.f) - 0.5f;
1359 vf = 0.5f * ehi * (vf + 1.f) - 0.5f;
1367 for (int i = -1; i < 3; i++) {
1368 for (int j = -1; j < 3; j++) {
1369 int new_ui = ui + j;
1370 int new_vi = vi + i;
1374 if (new_ui >= 0 && new_ui < ewi && new_vi >= 0 && new_vi < ehi) {
1375 face = s->in_cubemap_face_order[direction];
1377 u_shift = ceilf(ew * face);
1379 uf = 2.f * new_ui / ewi - 1.f;
1380 vf = 2.f * new_vi / ehi - 1.f;
1385 process_cube_coordinates(s, uf, vf, direction, &uf, &vf, &face);
1390 u_shift = ceilf(ew * face);
1391 new_ewi = ceilf(ew * (face + 1)) - u_shift;
1393 new_ui = av_clip(roundf(0.5f * new_ewi * (uf + 1.f)), 0, new_ewi - 1);
1394 new_vi = av_clip(roundf(0.5f * ehi * (vf + 1.f)), 0, ehi - 1);
1397 us[i + 1][j + 1] = u_shift + new_ui;
1398 vs[i + 1][j + 1] = new_vi;
1404 * Calculate 3D coordinates on sphere for corresponding frame position in equirectangular format.
1406 * @param s filter private context
1407 * @param i horizontal position on frame [0, width)
1408 * @param j vertical position on frame [0, height)
1409 * @param width frame width
1410 * @param height frame height
1411 * @param vec coordinates on sphere
1413 static void equirect_to_xyz(const V360Context *s,
1414 int i, int j, int width, int height,
1417 const float phi = ((2.f * i) / width - 1.f) * M_PI;
1418 const float theta = ((2.f * j) / height - 1.f) * M_PI_2;
1420 const float sin_phi = sinf(phi);
1421 const float cos_phi = cosf(phi);
1422 const float sin_theta = sinf(theta);
1423 const float cos_theta = cosf(theta);
1425 vec[0] = cos_theta * sin_phi;
1426 vec[1] = -sin_theta;
1427 vec[2] = -cos_theta * cos_phi;
1431 * Prepare data for processing stereographic output format.
1433 * @param ctx filter context
1435 * @return error code
1437 static int prepare_stereographic_out(AVFilterContext *ctx)
1439 V360Context *s = ctx->priv;
1441 s->flat_range[0] = tanf(FFMIN(s->h_fov, 359.f) * M_PI / 720.f);
1442 s->flat_range[1] = tanf(FFMIN(s->v_fov, 359.f) * M_PI / 720.f);
1448 * Calculate 3D coordinates on sphere for corresponding frame position in stereographic format.
1450 * @param s filter private context
1451 * @param i horizontal position on frame [0, width)
1452 * @param j vertical position on frame [0, height)
1453 * @param width frame width
1454 * @param height frame height
1455 * @param vec coordinates on sphere
1457 static void stereographic_to_xyz(const V360Context *s,
1458 int i, int j, int width, int height,
1461 const float x = ((2.f * i) / width - 1.f) * s->flat_range[0];
1462 const float y = ((2.f * j) / height - 1.f) * s->flat_range[1];
1463 const float xy = x * x + y * y;
1465 vec[0] = 2.f * x / (1.f + xy);
1466 vec[1] = (-1.f + xy) / (1.f + xy);
1467 vec[2] = 2.f * y / (1.f + xy);
1469 normalize_vector(vec);
1473 * Calculate frame position in stereographic format for corresponding 3D coordinates on sphere.
1475 * @param s filter private context
1476 * @param vec coordinates on sphere
1477 * @param width frame width
1478 * @param height frame height
1479 * @param us horizontal coordinates for interpolation window
1480 * @param vs vertical coordinates for interpolation window
1481 * @param du horizontal relative coordinate
1482 * @param dv vertical relative coordinate
1484 static void xyz_to_stereographic(const V360Context *s,
1485 const float *vec, int width, int height,
1486 uint16_t us[4][4], uint16_t vs[4][4], float *du, float *dv)
1488 const float x = av_clipf(vec[0] / (1.f - vec[1]), -1.f, 1.f) * s->input_mirror_modifier[0];
1489 const float y = av_clipf(vec[2] / (1.f - vec[1]), -1.f, 1.f) * s->input_mirror_modifier[1];
1493 uf = (x + 1.f) * width / 2.f;
1494 vf = (y + 1.f) * height / 2.f;
1501 for (int i = -1; i < 3; i++) {
1502 for (int j = -1; j < 3; j++) {
1503 us[i + 1][j + 1] = av_clip(ui + j, 0, width - 1);
1504 vs[i + 1][j + 1] = av_clip(vi + i, 0, height - 1);
1510 * Calculate frame position in equirectangular format for corresponding 3D coordinates on sphere.
1512 * @param s filter private context
1513 * @param vec coordinates on sphere
1514 * @param width frame width
1515 * @param height frame height
1516 * @param us horizontal coordinates for interpolation window
1517 * @param vs vertical coordinates for interpolation window
1518 * @param du horizontal relative coordinate
1519 * @param dv vertical relative coordinate
1521 static void xyz_to_equirect(const V360Context *s,
1522 const float *vec, int width, int height,
1523 uint16_t us[4][4], uint16_t vs[4][4], float *du, float *dv)
1525 const float phi = atan2f(vec[0], -vec[2]) * s->input_mirror_modifier[0];
1526 const float theta = asinf(-vec[1]) * s->input_mirror_modifier[1];
1530 uf = (phi / M_PI + 1.f) * width / 2.f;
1531 vf = (theta / M_PI_2 + 1.f) * height / 2.f;
1538 for (int i = -1; i < 3; i++) {
1539 for (int j = -1; j < 3; j++) {
1540 us[i + 1][j + 1] = mod(ui + j, width);
1541 vs[i + 1][j + 1] = av_clip(vi + i, 0, height - 1);
1547 * Calculate frame position in mercator format for corresponding 3D coordinates on sphere.
1549 * @param s filter private context
1550 * @param vec coordinates on sphere
1551 * @param width frame width
1552 * @param height frame height
1553 * @param us horizontal coordinates for interpolation window
1554 * @param vs vertical coordinates for interpolation window
1555 * @param du horizontal relative coordinate
1556 * @param dv vertical relative coordinate
1558 static void xyz_to_mercator(const V360Context *s,
1559 const float *vec, int width, int height,
1560 uint16_t us[4][4], uint16_t vs[4][4], float *du, float *dv)
1562 const float phi = atan2f(vec[0], -vec[2]) * s->input_mirror_modifier[0];
1563 const float theta = -vec[1] * s->input_mirror_modifier[1];
1567 uf = (phi / M_PI + 1.f) * width / 2.f;
1568 vf = (av_clipf(logf((1.f + theta) / (1.f - theta)) / (2.f * M_PI), -1.f, 1.f) + 1.f) * height / 2.f;
1575 for (int i = -1; i < 3; i++) {
1576 for (int j = -1; j < 3; j++) {
1577 us[i + 1][j + 1] = av_clip(ui + j, 0, width - 1);
1578 vs[i + 1][j + 1] = av_clip(vi + i, 0, height - 1);
1584 * Calculate 3D coordinates on sphere for corresponding frame position in mercator format.
1586 * @param s filter private context
1587 * @param i horizontal position on frame [0, width)
1588 * @param j vertical position on frame [0, height)
1589 * @param width frame width
1590 * @param height frame height
1591 * @param vec coordinates on sphere
1593 static void mercator_to_xyz(const V360Context *s,
1594 int i, int j, int width, int height,
1597 const float phi = ((2.f * i) / width - 1.f) * M_PI + M_PI_2;
1598 const float y = ((2.f * j) / height - 1.f) * M_PI;
1599 const float div = expf(2.f * y) + 1.f;
1601 const float sin_phi = sinf(phi);
1602 const float cos_phi = cosf(phi);
1603 const float sin_theta = -2.f * expf(y) / div;
1604 const float cos_theta = -(expf(2.f * y) - 1.f) / div;
1606 vec[0] = sin_theta * cos_phi;
1608 vec[2] = sin_theta * sin_phi;
1612 * Calculate frame position in ball format for corresponding 3D coordinates on sphere.
1614 * @param s filter private context
1615 * @param vec coordinates on sphere
1616 * @param width frame width
1617 * @param height frame height
1618 * @param us horizontal coordinates for interpolation window
1619 * @param vs vertical coordinates for interpolation window
1620 * @param du horizontal relative coordinate
1621 * @param dv vertical relative coordinate
1623 static void xyz_to_ball(const V360Context *s,
1624 const float *vec, int width, int height,
1625 uint16_t us[4][4], uint16_t vs[4][4], float *du, float *dv)
1627 const float l = hypotf(vec[0], vec[1]);
1628 const float r = sqrtf(1.f + vec[2]) / M_SQRT2;
1632 uf = (1.f + r * vec[0] * s->input_mirror_modifier[0] / (l > 0.f ? l : 1.f)) * width * 0.5f;
1633 vf = (1.f - r * vec[1] * s->input_mirror_modifier[1] / (l > 0.f ? l : 1.f)) * height * 0.5f;
1641 for (int i = -1; i < 3; i++) {
1642 for (int j = -1; j < 3; j++) {
1643 us[i + 1][j + 1] = av_clip(ui + j, 0, width - 1);
1644 vs[i + 1][j + 1] = av_clip(vi + i, 0, height - 1);
1650 * Calculate 3D coordinates on sphere for corresponding frame position in ball format.
1652 * @param s filter private context
1653 * @param i horizontal position on frame [0, width)
1654 * @param j vertical position on frame [0, height)
1655 * @param width frame width
1656 * @param height frame height
1657 * @param vec coordinates on sphere
1659 static void ball_to_xyz(const V360Context *s,
1660 int i, int j, int width, int height,
1663 const float x = (2.f * i) / width - 1.f;
1664 const float y = (2.f * j) / height - 1.f;
1665 const float l = hypotf(x, y);
1668 const float z = 2.f * l * sqrtf(1.f - l * l);
1670 vec[0] = z * x / (l > 0.f ? l : 1.f);
1671 vec[1] = -z * y / (l > 0.f ? l : 1.f);
1672 vec[2] = -1.f + 2.f * l * l;
1681 * Calculate 3D coordinates on sphere for corresponding frame position in hammer format.
1683 * @param s filter private context
1684 * @param i horizontal position on frame [0, width)
1685 * @param j vertical position on frame [0, height)
1686 * @param width frame width
1687 * @param height frame height
1688 * @param vec coordinates on sphere
1690 static void hammer_to_xyz(const V360Context *s,
1691 int i, int j, int width, int height,
1694 const float x = ((2.f * i) / width - 1.f);
1695 const float y = ((2.f * j) / height - 1.f);
1697 const float xx = x * x;
1698 const float yy = y * y;
1700 const float z = sqrtf(1.f - xx * 0.5f - yy * 0.5f);
1702 const float a = M_SQRT2 * x * z;
1703 const float b = 2.f * z * z - 1.f;
1705 const float aa = a * a;
1706 const float bb = b * b;
1708 const float w = sqrtf(1.f - 2.f * yy * z * z);
1710 vec[0] = w * 2.f * a * b / (aa + bb);
1711 vec[1] = -M_SQRT2 * y * z;
1712 vec[2] = -w * (bb - aa) / (aa + bb);
1714 normalize_vector(vec);
1718 * Calculate frame position in hammer format for corresponding 3D coordinates on sphere.
1720 * @param s filter private context
1721 * @param vec coordinates on sphere
1722 * @param width frame width
1723 * @param height frame height
1724 * @param us horizontal coordinates for interpolation window
1725 * @param vs vertical coordinates for interpolation window
1726 * @param du horizontal relative coordinate
1727 * @param dv vertical relative coordinate
1729 static void xyz_to_hammer(const V360Context *s,
1730 const float *vec, int width, int height,
1731 uint16_t us[4][4], uint16_t vs[4][4], float *du, float *dv)
1733 const float theta = atan2f(vec[0], -vec[2]) * s->input_mirror_modifier[0];
1735 const float z = sqrtf(1.f + sqrtf(1.f - vec[1] * vec[1]) * cosf(theta * 0.5f));
1736 const float x = sqrtf(1.f - vec[1] * vec[1]) * sinf(theta * 0.5f) / z;
1737 const float y = -vec[1] / z * s->input_mirror_modifier[1];
1741 uf = (x + 1.f) * width / 2.f;
1742 vf = (y + 1.f) * height / 2.f;
1749 for (int i = -1; i < 3; i++) {
1750 for (int j = -1; j < 3; j++) {
1751 us[i + 1][j + 1] = av_clip(ui + j, 0, width - 1);
1752 vs[i + 1][j + 1] = av_clip(vi + i, 0, height - 1);
1758 * Calculate 3D coordinates on sphere for corresponding frame position in sinusoidal format.
1760 * @param s filter private context
1761 * @param i horizontal position on frame [0, width)
1762 * @param j vertical position on frame [0, height)
1763 * @param width frame width
1764 * @param height frame height
1765 * @param vec coordinates on sphere
1767 static void sinusoidal_to_xyz(const V360Context *s,
1768 int i, int j, int width, int height,
1771 const float theta = ((2.f * j) / height - 1.f) * M_PI_2;
1772 const float phi = ((2.f * i) / width - 1.f) * M_PI / cosf(theta);
1774 const float sin_phi = sinf(phi);
1775 const float cos_phi = cosf(phi);
1776 const float sin_theta = sinf(theta);
1777 const float cos_theta = cosf(theta);
1779 vec[0] = cos_theta * sin_phi;
1780 vec[1] = -sin_theta;
1781 vec[2] = -cos_theta * cos_phi;
1783 normalize_vector(vec);
1787 * Calculate frame position in sinusoidal format for corresponding 3D coordinates on sphere.
1789 * @param s filter private context
1790 * @param vec coordinates on sphere
1791 * @param width frame width
1792 * @param height frame height
1793 * @param us horizontal coordinates for interpolation window
1794 * @param vs vertical coordinates for interpolation window
1795 * @param du horizontal relative coordinate
1796 * @param dv vertical relative coordinate
1798 static void xyz_to_sinusoidal(const V360Context *s,
1799 const float *vec, int width, int height,
1800 uint16_t us[4][4], uint16_t vs[4][4], float *du, float *dv)
1802 const float theta = asinf(-vec[1]) * s->input_mirror_modifier[1];
1803 const float phi = atan2f(vec[0], -vec[2]) * s->input_mirror_modifier[0] * cosf(theta);
1807 uf = (phi / M_PI + 1.f) * width / 2.f;
1808 vf = (theta / M_PI_2 + 1.f) * height / 2.f;
1815 for (int i = -1; i < 3; i++) {
1816 for (int j = -1; j < 3; j++) {
1817 us[i + 1][j + 1] = av_clip(ui + j, 0, width - 1);
1818 vs[i + 1][j + 1] = av_clip(vi + i, 0, height - 1);
1824 * Prepare data for processing equi-angular cubemap input format.
1826 * @param ctx filter context
1828 * @return error code
1830 static int prepare_eac_in(AVFilterContext *ctx)
1832 V360Context *s = ctx->priv;
1834 if (s->ih_flip && s->iv_flip) {
1835 s->in_cubemap_face_order[RIGHT] = BOTTOM_LEFT;
1836 s->in_cubemap_face_order[LEFT] = BOTTOM_RIGHT;
1837 s->in_cubemap_face_order[UP] = TOP_LEFT;
1838 s->in_cubemap_face_order[DOWN] = TOP_RIGHT;
1839 s->in_cubemap_face_order[FRONT] = BOTTOM_MIDDLE;
1840 s->in_cubemap_face_order[BACK] = TOP_MIDDLE;
1841 } else if (s->ih_flip) {
1842 s->in_cubemap_face_order[RIGHT] = TOP_LEFT;
1843 s->in_cubemap_face_order[LEFT] = TOP_RIGHT;
1844 s->in_cubemap_face_order[UP] = BOTTOM_LEFT;
1845 s->in_cubemap_face_order[DOWN] = BOTTOM_RIGHT;
1846 s->in_cubemap_face_order[FRONT] = TOP_MIDDLE;
1847 s->in_cubemap_face_order[BACK] = BOTTOM_MIDDLE;
1848 } else if (s->iv_flip) {
1849 s->in_cubemap_face_order[RIGHT] = BOTTOM_RIGHT;
1850 s->in_cubemap_face_order[LEFT] = BOTTOM_LEFT;
1851 s->in_cubemap_face_order[UP] = TOP_RIGHT;
1852 s->in_cubemap_face_order[DOWN] = TOP_LEFT;
1853 s->in_cubemap_face_order[FRONT] = BOTTOM_MIDDLE;
1854 s->in_cubemap_face_order[BACK] = TOP_MIDDLE;
1856 s->in_cubemap_face_order[RIGHT] = TOP_RIGHT;
1857 s->in_cubemap_face_order[LEFT] = TOP_LEFT;
1858 s->in_cubemap_face_order[UP] = BOTTOM_RIGHT;
1859 s->in_cubemap_face_order[DOWN] = BOTTOM_LEFT;
1860 s->in_cubemap_face_order[FRONT] = TOP_MIDDLE;
1861 s->in_cubemap_face_order[BACK] = BOTTOM_MIDDLE;
1865 s->in_cubemap_face_rotation[TOP_LEFT] = ROT_270;
1866 s->in_cubemap_face_rotation[TOP_MIDDLE] = ROT_90;
1867 s->in_cubemap_face_rotation[TOP_RIGHT] = ROT_270;
1868 s->in_cubemap_face_rotation[BOTTOM_LEFT] = ROT_0;
1869 s->in_cubemap_face_rotation[BOTTOM_MIDDLE] = ROT_0;
1870 s->in_cubemap_face_rotation[BOTTOM_RIGHT] = ROT_0;
1872 s->in_cubemap_face_rotation[TOP_LEFT] = ROT_0;
1873 s->in_cubemap_face_rotation[TOP_MIDDLE] = ROT_0;
1874 s->in_cubemap_face_rotation[TOP_RIGHT] = ROT_0;
1875 s->in_cubemap_face_rotation[BOTTOM_LEFT] = ROT_270;
1876 s->in_cubemap_face_rotation[BOTTOM_MIDDLE] = ROT_90;
1877 s->in_cubemap_face_rotation[BOTTOM_RIGHT] = ROT_270;
1884 * Prepare data for processing equi-angular cubemap output format.
1886 * @param ctx filter context
1888 * @return error code
1890 static int prepare_eac_out(AVFilterContext *ctx)
1892 V360Context *s = ctx->priv;
1894 s->out_cubemap_direction_order[TOP_LEFT] = LEFT;
1895 s->out_cubemap_direction_order[TOP_MIDDLE] = FRONT;
1896 s->out_cubemap_direction_order[TOP_RIGHT] = RIGHT;
1897 s->out_cubemap_direction_order[BOTTOM_LEFT] = DOWN;
1898 s->out_cubemap_direction_order[BOTTOM_MIDDLE] = BACK;
1899 s->out_cubemap_direction_order[BOTTOM_RIGHT] = UP;
1901 s->out_cubemap_face_rotation[TOP_LEFT] = ROT_0;
1902 s->out_cubemap_face_rotation[TOP_MIDDLE] = ROT_0;
1903 s->out_cubemap_face_rotation[TOP_RIGHT] = ROT_0;
1904 s->out_cubemap_face_rotation[BOTTOM_LEFT] = ROT_270;
1905 s->out_cubemap_face_rotation[BOTTOM_MIDDLE] = ROT_90;
1906 s->out_cubemap_face_rotation[BOTTOM_RIGHT] = ROT_270;
1912 * Calculate 3D coordinates on sphere for corresponding frame position in equi-angular cubemap format.
1914 * @param s filter private context
1915 * @param i horizontal position on frame [0, width)
1916 * @param j vertical position on frame [0, height)
1917 * @param width frame width
1918 * @param height frame height
1919 * @param vec coordinates on sphere
1921 static void eac_to_xyz(const V360Context *s,
1922 int i, int j, int width, int height,
1925 const float pixel_pad = 2;
1926 const float u_pad = pixel_pad / width;
1927 const float v_pad = pixel_pad / height;
1929 int u_face, v_face, face;
1931 float l_x, l_y, l_z;
1933 float uf = (i + 0.5f) / width;
1934 float vf = (j + 0.5f) / height;
1936 // EAC has 2-pixel padding on faces except between faces on the same row
1937 // Padding pixels seems not to be stretched with tangent as regular pixels
1938 // Formulas below approximate original padding as close as I could get experimentally
1940 // Horizontal padding
1941 uf = 3.f * (uf - u_pad) / (1.f - 2.f * u_pad);
1945 } else if (uf >= 3.f) {
1949 u_face = floorf(uf);
1950 uf = fmodf(uf, 1.f) - 0.5f;
1954 v_face = floorf(vf * 2.f);
1955 vf = (vf - v_pad - 0.5f * v_face) / (0.5f - 2.f * v_pad) - 0.5f;
1957 if (uf >= -0.5f && uf < 0.5f) {
1958 uf = tanf(M_PI_2 * uf);
1962 if (vf >= -0.5f && vf < 0.5f) {
1963 vf = tanf(M_PI_2 * vf);
1968 face = u_face + 3 * v_face;
2009 normalize_vector(vec);
2013 * Calculate frame position in equi-angular cubemap format for corresponding 3D coordinates on sphere.
2015 * @param s filter private context
2016 * @param vec coordinates on sphere
2017 * @param width frame width
2018 * @param height frame height
2019 * @param us horizontal coordinates for interpolation window
2020 * @param vs vertical coordinates for interpolation window
2021 * @param du horizontal relative coordinate
2022 * @param dv vertical relative coordinate
2024 static void xyz_to_eac(const V360Context *s,
2025 const float *vec, int width, int height,
2026 uint16_t us[4][4], uint16_t vs[4][4], float *du, float *dv)
2028 const float pixel_pad = 2;
2029 const float u_pad = pixel_pad / width;
2030 const float v_pad = pixel_pad / height;
2034 int direction, face;
2037 xyz_to_cube(s, vec, &uf, &vf, &direction);
2039 face = s->in_cubemap_face_order[direction];
2043 uf = M_2_PI * atanf(uf) + 0.5f;
2044 vf = M_2_PI * atanf(vf) + 0.5f;
2046 // These formulas are inversed from eac_to_xyz ones
2047 uf = (uf + u_face) * (1.f - 2.f * u_pad) / 3.f + u_pad;
2048 vf = vf * (0.5f - 2.f * v_pad) + v_pad + 0.5f * v_face;
2062 for (int i = -1; i < 3; i++) {
2063 for (int j = -1; j < 3; j++) {
2064 us[i + 1][j + 1] = av_clip(ui + j, 0, width - 1);
2065 vs[i + 1][j + 1] = av_clip(vi + i, 0, height - 1);
2071 * Prepare data for processing flat output format.
2073 * @param ctx filter context
2075 * @return error code
2077 static int prepare_flat_out(AVFilterContext *ctx)
2079 V360Context *s = ctx->priv;
2081 s->flat_range[0] = tanf(0.5f * s->h_fov * M_PI / 180.f);
2082 s->flat_range[1] = tanf(0.5f * s->v_fov * M_PI / 180.f);
2088 * Calculate 3D coordinates on sphere for corresponding frame position in flat format.
2090 * @param s filter private context
2091 * @param i horizontal position on frame [0, width)
2092 * @param j vertical position on frame [0, height)
2093 * @param width frame width
2094 * @param height frame height
2095 * @param vec coordinates on sphere
2097 static void flat_to_xyz(const V360Context *s,
2098 int i, int j, int width, int height,
2101 const float l_x = s->flat_range[0] * (2.f * i / width - 1.f);
2102 const float l_y = -s->flat_range[1] * (2.f * j / height - 1.f);
2108 normalize_vector(vec);
2112 * Prepare data for processing fisheye output format.
2114 * @param ctx filter context
2116 * @return error code
2118 static int prepare_fisheye_out(AVFilterContext *ctx)
2120 V360Context *s = ctx->priv;
2122 s->flat_range[0] = s->h_fov / 180.f;
2123 s->flat_range[1] = s->v_fov / 180.f;
2129 * Calculate 3D coordinates on sphere for corresponding frame position in fisheye format.
2131 * @param s filter private context
2132 * @param i horizontal position on frame [0, width)
2133 * @param j vertical position on frame [0, height)
2134 * @param width frame width
2135 * @param height frame height
2136 * @param vec coordinates on sphere
2138 static void fisheye_to_xyz(const V360Context *s,
2139 int i, int j, int width, int height,
2142 const float uf = s->flat_range[0] * ((2.f * i) / width - 1.f);
2143 const float vf = s->flat_range[1] * ((2.f * j) / height - 1.f);
2145 const float phi = -atan2f(vf, uf);
2146 const float theta = -M_PI_2 * (1.f - hypotf(uf, vf));
2148 vec[0] = cosf(theta) * cosf(phi);
2149 vec[1] = cosf(theta) * sinf(phi);
2150 vec[2] = sinf(theta);
2152 normalize_vector(vec);
2156 * Calculate 3D coordinates on sphere for corresponding frame position in pannini format.
2158 * @param s filter private context
2159 * @param i horizontal position on frame [0, width)
2160 * @param j vertical position on frame [0, height)
2161 * @param width frame width
2162 * @param height frame height
2163 * @param vec coordinates on sphere
2165 static void pannini_to_xyz(const V360Context *s,
2166 int i, int j, int width, int height,
2169 const float uf = ((2.f * i) / width - 1.f);
2170 const float vf = ((2.f * j) / height - 1.f);
2172 const float d = s->h_fov;
2173 float k = uf * uf / ((d + 1.f) * (d + 1.f));
2174 float dscr = k * k * d * d - (k + 1) * (k * d * d - 1.f);
2175 float clon = (-k * d + sqrtf(dscr)) / (k + 1.f);
2176 float S = (d + 1.f) / (d + clon);
2177 float lon = -(M_PI + atan2f(uf, S * clon));
2178 float lat = -atan2f(vf, S);
2180 vec[0] = sinf(lon) * cosf(lat);
2182 vec[2] = cosf(lon) * cosf(lat);
2184 normalize_vector(vec);
2188 * Prepare data for processing cylindrical output format.
2190 * @param ctx filter context
2192 * @return error code
2194 static int prepare_cylindrical_out(AVFilterContext *ctx)
2196 V360Context *s = ctx->priv;
2198 s->flat_range[0] = M_PI * s->h_fov / 360.f;
2199 s->flat_range[1] = tanf(0.5f * s->v_fov * M_PI / 180.f);
2205 * Calculate 3D coordinates on sphere for corresponding frame position in cylindrical format.
2207 * @param s filter private context
2208 * @param i horizontal position on frame [0, width)
2209 * @param j vertical position on frame [0, height)
2210 * @param width frame width
2211 * @param height frame height
2212 * @param vec coordinates on sphere
2214 static void cylindrical_to_xyz(const V360Context *s,
2215 int i, int j, int width, int height,
2218 const float uf = s->flat_range[0] * ((2.f * i) / width - 1.f);
2219 const float vf = s->flat_range[1] * ((2.f * j) / height - 1.f);
2221 const float phi = uf;
2222 const float theta = atanf(vf);
2224 const float sin_phi = sinf(phi);
2225 const float cos_phi = cosf(phi);
2226 const float sin_theta = sinf(theta);
2227 const float cos_theta = cosf(theta);
2229 vec[0] = cos_theta * sin_phi;
2230 vec[1] = -sin_theta;
2231 vec[2] = -cos_theta * cos_phi;
2233 normalize_vector(vec);
2237 * Calculate 3D coordinates on sphere for corresponding frame position in dual fisheye format.
2239 * @param s filter private context
2240 * @param i horizontal position on frame [0, width)
2241 * @param j vertical position on frame [0, height)
2242 * @param width frame width
2243 * @param height frame height
2244 * @param vec coordinates on sphere
2246 static void dfisheye_to_xyz(const V360Context *s,
2247 int i, int j, int width, int height,
2250 const float scale = 1.f + s->out_pad;
2252 const float ew = width / 2.f;
2253 const float eh = height;
2255 const int ei = i >= ew ? i - ew : i;
2256 const float m = i >= ew ? -1.f : 1.f;
2258 const float uf = ((2.f * ei) / ew - 1.f) * scale;
2259 const float vf = ((2.f * j) / eh - 1.f) * scale;
2261 const float h = hypotf(uf, vf);
2262 const float lh = h > 0.f ? h : 1.f;
2263 const float theta = m * M_PI_2 * (1.f - h);
2265 const float sin_theta = sinf(theta);
2266 const float cos_theta = cosf(theta);
2268 vec[0] = cos_theta * m * -uf / lh;
2269 vec[1] = cos_theta * -vf / lh;
2272 normalize_vector(vec);
2276 * Calculate frame position in dual fisheye format for corresponding 3D coordinates on sphere.
2278 * @param s filter private context
2279 * @param vec coordinates on sphere
2280 * @param width frame width
2281 * @param height frame height
2282 * @param us horizontal coordinates for interpolation window
2283 * @param vs vertical coordinates for interpolation window
2284 * @param du horizontal relative coordinate
2285 * @param dv vertical relative coordinate
2287 static void xyz_to_dfisheye(const V360Context *s,
2288 const float *vec, int width, int height,
2289 uint16_t us[4][4], uint16_t vs[4][4], float *du, float *dv)
2291 const float scale = 1.f - s->in_pad;
2293 const float ew = width / 2.f;
2294 const float eh = height;
2296 const float h = hypotf(vec[0], vec[1]);
2297 const float lh = h > 0.f ? h : 1.f;
2298 const float theta = acosf(fabsf(vec[2])) / M_PI;
2300 float uf = (theta * (-vec[0] / lh) * s->input_mirror_modifier[0] * scale + 0.5f) * ew;
2301 float vf = (theta * (-vec[1] / lh) * s->input_mirror_modifier[1] * scale + 0.5f) * eh;
2306 if (vec[2] >= 0.f) {
2309 u_shift = ceilf(ew);
2319 for (int i = -1; i < 3; i++) {
2320 for (int j = -1; j < 3; j++) {
2321 us[i + 1][j + 1] = av_clip(u_shift + ui + j, 0, width - 1);
2322 vs[i + 1][j + 1] = av_clip( vi + i, 0, height - 1);
2328 * Calculate 3D coordinates on sphere for corresponding frame position in barrel facebook's format.
2330 * @param s filter private context
2331 * @param i horizontal position on frame [0, width)
2332 * @param j vertical position on frame [0, height)
2333 * @param width frame width
2334 * @param height frame height
2335 * @param vec coordinates on sphere
2337 static void barrel_to_xyz(const V360Context *s,
2338 int i, int j, int width, int height,
2341 const float scale = 0.99f;
2342 float l_x, l_y, l_z;
2344 if (i < 4 * width / 5) {
2345 const float theta_range = M_PI_4;
2347 const int ew = 4 * width / 5;
2348 const int eh = height;
2350 const float phi = ((2.f * i) / ew - 1.f) * M_PI / scale;
2351 const float theta = ((2.f * j) / eh - 1.f) * theta_range / scale;
2353 const float sin_phi = sinf(phi);
2354 const float cos_phi = cosf(phi);
2355 const float sin_theta = sinf(theta);
2356 const float cos_theta = cosf(theta);
2358 l_x = cos_theta * sin_phi;
2360 l_z = -cos_theta * cos_phi;
2362 const int ew = width / 5;
2363 const int eh = height / 2;
2368 uf = 2.f * (i - 4 * ew) / ew - 1.f;
2369 vf = 2.f * (j ) / eh - 1.f;
2378 uf = 2.f * (i - 4 * ew) / ew - 1.f;
2379 vf = 2.f * (j - eh) / eh - 1.f;
2394 normalize_vector(vec);
2398 * Calculate frame position in barrel facebook's format for corresponding 3D coordinates on sphere.
2400 * @param s filter private context
2401 * @param vec coordinates on sphere
2402 * @param width frame width
2403 * @param height frame height
2404 * @param us horizontal coordinates for interpolation window
2405 * @param vs vertical coordinates for interpolation window
2406 * @param du horizontal relative coordinate
2407 * @param dv vertical relative coordinate
2409 static void xyz_to_barrel(const V360Context *s,
2410 const float *vec, int width, int height,
2411 uint16_t us[4][4], uint16_t vs[4][4], float *du, float *dv)
2413 const float scale = 0.99f;
2415 const float phi = atan2f(vec[0], -vec[2]) * s->input_mirror_modifier[0];
2416 const float theta = asinf(-vec[1]) * s->input_mirror_modifier[1];
2417 const float theta_range = M_PI_4;
2420 int u_shift, v_shift;
2424 if (theta > -theta_range && theta < theta_range) {
2428 u_shift = s->ih_flip ? width / 5 : 0;
2431 uf = (phi / M_PI * scale + 1.f) * ew / 2.f;
2432 vf = (theta / theta_range * scale + 1.f) * eh / 2.f;
2437 u_shift = s->ih_flip ? 0 : 4 * ew;
2439 if (theta < 0.f) { // UP
2440 uf = vec[0] / vec[1];
2441 vf = -vec[2] / vec[1];
2444 uf = -vec[0] / vec[1];
2445 vf = -vec[2] / vec[1];
2449 uf *= s->input_mirror_modifier[0] * s->input_mirror_modifier[1];
2450 vf *= s->input_mirror_modifier[1];
2452 uf = 0.5f * ew * (uf * scale + 1.f);
2453 vf = 0.5f * eh * (vf * scale + 1.f);
2462 for (int i = -1; i < 3; i++) {
2463 for (int j = -1; j < 3; j++) {
2464 us[i + 1][j + 1] = u_shift + av_clip(ui + j, 0, ew - 1);
2465 vs[i + 1][j + 1] = v_shift + av_clip(vi + i, 0, eh - 1);
2470 static void multiply_matrix(float c[3][3], const float a[3][3], const float b[3][3])
2472 for (int i = 0; i < 3; i++) {
2473 for (int j = 0; j < 3; j++) {
2476 for (int k = 0; k < 3; k++)
2477 sum += a[i][k] * b[k][j];
2485 * Calculate rotation matrix for yaw/pitch/roll angles.
2487 static inline void calculate_rotation_matrix(float yaw, float pitch, float roll,
2488 float rot_mat[3][3],
2489 const int rotation_order[3])
2491 const float yaw_rad = yaw * M_PI / 180.f;
2492 const float pitch_rad = pitch * M_PI / 180.f;
2493 const float roll_rad = roll * M_PI / 180.f;
2495 const float sin_yaw = sinf(-yaw_rad);
2496 const float cos_yaw = cosf(-yaw_rad);
2497 const float sin_pitch = sinf(pitch_rad);
2498 const float cos_pitch = cosf(pitch_rad);
2499 const float sin_roll = sinf(roll_rad);
2500 const float cos_roll = cosf(roll_rad);
2505 m[0][0][0] = cos_yaw; m[0][0][1] = 0; m[0][0][2] = sin_yaw;
2506 m[0][1][0] = 0; m[0][1][1] = 1; m[0][1][2] = 0;
2507 m[0][2][0] = -sin_yaw; m[0][2][1] = 0; m[0][2][2] = cos_yaw;
2509 m[1][0][0] = 1; m[1][0][1] = 0; m[1][0][2] = 0;
2510 m[1][1][0] = 0; m[1][1][1] = cos_pitch; m[1][1][2] = -sin_pitch;
2511 m[1][2][0] = 0; m[1][2][1] = sin_pitch; m[1][2][2] = cos_pitch;
2513 m[2][0][0] = cos_roll; m[2][0][1] = -sin_roll; m[2][0][2] = 0;
2514 m[2][1][0] = sin_roll; m[2][1][1] = cos_roll; m[2][1][2] = 0;
2515 m[2][2][0] = 0; m[2][2][1] = 0; m[2][2][2] = 1;
2517 multiply_matrix(temp, m[rotation_order[0]], m[rotation_order[1]]);
2518 multiply_matrix(rot_mat, temp, m[rotation_order[2]]);
2522 * Rotate vector with given rotation matrix.
2524 * @param rot_mat rotation matrix
2527 static inline void rotate(const float rot_mat[3][3],
2530 const float x_tmp = vec[0] * rot_mat[0][0] + vec[1] * rot_mat[0][1] + vec[2] * rot_mat[0][2];
2531 const float y_tmp = vec[0] * rot_mat[1][0] + vec[1] * rot_mat[1][1] + vec[2] * rot_mat[1][2];
2532 const float z_tmp = vec[0] * rot_mat[2][0] + vec[1] * rot_mat[2][1] + vec[2] * rot_mat[2][2];
2539 static inline void set_mirror_modifier(int h_flip, int v_flip, int d_flip,
2542 modifier[0] = h_flip ? -1.f : 1.f;
2543 modifier[1] = v_flip ? -1.f : 1.f;
2544 modifier[2] = d_flip ? -1.f : 1.f;
2547 static inline void mirror(const float *modifier, float *vec)
2549 vec[0] *= modifier[0];
2550 vec[1] *= modifier[1];
2551 vec[2] *= modifier[2];
2554 static int allocate_plane(V360Context *s, int sizeof_uv, int sizeof_ker, int p)
2556 s->u[p] = av_calloc(s->uv_linesize[p] * s->pr_height[p], sizeof_uv);
2557 s->v[p] = av_calloc(s->uv_linesize[p] * s->pr_height[p], sizeof_uv);
2558 if (!s->u[p] || !s->v[p])
2559 return AVERROR(ENOMEM);
2561 s->ker[p] = av_calloc(s->uv_linesize[p] * s->pr_height[p], sizeof_ker);
2563 return AVERROR(ENOMEM);
2569 static void fov_from_dfov(V360Context *s, float w, float h)
2571 const float da = tanf(0.5 * FFMIN(s->d_fov, 359.f) * M_PI / 180.f);
2572 const float d = hypotf(w, h);
2574 s->h_fov = atan2f(da * w, d) * 360.f / M_PI;
2575 s->v_fov = atan2f(da * h, d) * 360.f / M_PI;
2583 static void set_dimensions(int *outw, int *outh, int w, int h, const AVPixFmtDescriptor *desc)
2585 outw[1] = outw[2] = FF_CEIL_RSHIFT(w, desc->log2_chroma_w);
2586 outw[0] = outw[3] = w;
2587 outh[1] = outh[2] = FF_CEIL_RSHIFT(h, desc->log2_chroma_h);
2588 outh[0] = outh[3] = h;
2591 // Calculate remap data
2592 static av_always_inline int v360_slice(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
2594 V360Context *s = ctx->priv;
2596 for (int p = 0; p < s->nb_allocated; p++) {
2597 const int width = s->pr_width[p];
2598 const int uv_linesize = s->uv_linesize[p];
2599 const int height = s->pr_height[p];
2600 const int in_width = s->inplanewidth[p];
2601 const int in_height = s->inplaneheight[p];
2602 const int slice_start = (height * jobnr ) / nb_jobs;
2603 const int slice_end = (height * (jobnr + 1)) / nb_jobs;
2608 for (int j = slice_start; j < slice_end; j++) {
2609 for (int i = 0; i < width; i++) {
2610 uint16_t *u = s->u[p] + (j * uv_linesize + i) * s->elements;
2611 uint16_t *v = s->v[p] + (j * uv_linesize + i) * s->elements;
2612 int16_t *ker = s->ker[p] + (j * uv_linesize + i) * s->elements;
2614 if (s->out_transpose)
2615 s->out_transform(s, j, i, height, width, vec);
2617 s->out_transform(s, i, j, width, height, vec);
2618 av_assert1(!isnan(vec[0]) && !isnan(vec[1]) && !isnan(vec[2]));
2619 rotate(s->rot_mat, vec);
2620 av_assert1(!isnan(vec[0]) && !isnan(vec[1]) && !isnan(vec[2]));
2621 normalize_vector(vec);
2622 mirror(s->output_mirror_modifier, vec);
2623 if (s->in_transpose)
2624 s->in_transform(s, vec, in_height, in_width, rmap.v, rmap.u, &du, &dv);
2626 s->in_transform(s, vec, in_width, in_height, rmap.u, rmap.v, &du, &dv);
2627 av_assert1(!isnan(du) && !isnan(dv));
2628 s->calculate_kernel(du, dv, &rmap, u, v, ker);
2636 static int config_output(AVFilterLink *outlink)
2638 AVFilterContext *ctx = outlink->src;
2639 AVFilterLink *inlink = ctx->inputs[0];
2640 V360Context *s = ctx->priv;
2641 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(inlink->format);
2642 const int depth = desc->comp[0].depth;
2647 int in_offset_h, in_offset_w;
2648 int out_offset_h, out_offset_w;
2650 int (*prepare_out)(AVFilterContext *ctx);
2652 s->input_mirror_modifier[0] = s->ih_flip ? -1.f : 1.f;
2653 s->input_mirror_modifier[1] = s->iv_flip ? -1.f : 1.f;
2655 switch (s->interp) {
2657 s->calculate_kernel = nearest_kernel;
2658 s->remap_slice = depth <= 8 ? remap1_8bit_slice : remap1_16bit_slice;
2660 sizeof_uv = sizeof(uint16_t) * s->elements;
2664 s->calculate_kernel = bilinear_kernel;
2665 s->remap_slice = depth <= 8 ? remap2_8bit_slice : remap2_16bit_slice;
2666 s->elements = 2 * 2;
2667 sizeof_uv = sizeof(uint16_t) * s->elements;
2668 sizeof_ker = sizeof(uint16_t) * s->elements;
2671 s->calculate_kernel = bicubic_kernel;
2672 s->remap_slice = depth <= 8 ? remap4_8bit_slice : remap4_16bit_slice;
2673 s->elements = 4 * 4;
2674 sizeof_uv = sizeof(uint16_t) * s->elements;
2675 sizeof_ker = sizeof(uint16_t) * s->elements;
2678 s->calculate_kernel = lanczos_kernel;
2679 s->remap_slice = depth <= 8 ? remap4_8bit_slice : remap4_16bit_slice;
2680 s->elements = 4 * 4;
2681 sizeof_uv = sizeof(uint16_t) * s->elements;
2682 sizeof_ker = sizeof(uint16_t) * s->elements;
2688 ff_v360_init(s, depth);
2690 for (int order = 0; order < NB_RORDERS; order++) {
2691 const char c = s->rorder[order];
2695 av_log(ctx, AV_LOG_ERROR,
2696 "Incomplete rorder option. Direction for all 3 rotation orders should be specified.\n");
2697 return AVERROR(EINVAL);
2700 rorder = get_rorder(c);
2702 av_log(ctx, AV_LOG_ERROR,
2703 "Incorrect rotation order symbol '%c' in rorder option.\n", c);
2704 return AVERROR(EINVAL);
2707 s->rotation_order[order] = rorder;
2710 switch (s->in_stereo) {
2714 in_offset_w = in_offset_h = 0;
2732 set_dimensions(s->inplanewidth, s->inplaneheight, w, h, desc);
2733 set_dimensions(s->in_offset_w, s->in_offset_h, in_offset_w, in_offset_h, desc);
2735 s->in_width = s->inplanewidth[0];
2736 s->in_height = s->inplaneheight[0];
2738 if (s->in_transpose)
2739 FFSWAP(int, s->in_width, s->in_height);
2742 case EQUIRECTANGULAR:
2743 s->in_transform = xyz_to_equirect;
2749 s->in_transform = xyz_to_cube3x2;
2750 err = prepare_cube_in(ctx);
2755 s->in_transform = xyz_to_cube1x6;
2756 err = prepare_cube_in(ctx);
2761 s->in_transform = xyz_to_cube6x1;
2762 err = prepare_cube_in(ctx);
2767 s->in_transform = xyz_to_eac;
2768 err = prepare_eac_in(ctx);
2776 av_log(ctx, AV_LOG_ERROR, "Supplied format is not accepted as input.\n");
2777 return AVERROR(EINVAL);
2779 s->in_transform = xyz_to_dfisheye;
2785 s->in_transform = xyz_to_barrel;
2791 s->in_transform = xyz_to_stereographic;
2797 s->in_transform = xyz_to_mercator;
2803 s->in_transform = xyz_to_ball;
2809 s->in_transform = xyz_to_hammer;
2815 s->in_transform = xyz_to_sinusoidal;
2821 av_log(ctx, AV_LOG_ERROR, "Specified input format is not handled.\n");
2830 case EQUIRECTANGULAR:
2831 s->out_transform = equirect_to_xyz;
2837 s->out_transform = cube3x2_to_xyz;
2838 prepare_out = prepare_cube_out;
2839 w = roundf(wf / 4.f * 3.f);
2843 s->out_transform = cube1x6_to_xyz;
2844 prepare_out = prepare_cube_out;
2845 w = roundf(wf / 4.f);
2846 h = roundf(hf * 3.f);
2849 s->out_transform = cube6x1_to_xyz;
2850 prepare_out = prepare_cube_out;
2851 w = roundf(wf / 2.f * 3.f);
2852 h = roundf(hf / 2.f);
2855 s->out_transform = eac_to_xyz;
2856 prepare_out = prepare_eac_out;
2858 h = roundf(hf / 8.f * 9.f);
2861 s->out_transform = flat_to_xyz;
2862 prepare_out = prepare_flat_out;
2867 s->out_transform = dfisheye_to_xyz;
2873 s->out_transform = barrel_to_xyz;
2875 w = roundf(wf / 4.f * 5.f);
2879 s->out_transform = stereographic_to_xyz;
2880 prepare_out = prepare_stereographic_out;
2882 h = roundf(hf * 2.f);
2885 s->out_transform = mercator_to_xyz;
2888 h = roundf(hf * 2.f);
2891 s->out_transform = ball_to_xyz;
2894 h = roundf(hf * 2.f);
2897 s->out_transform = hammer_to_xyz;
2903 s->out_transform = sinusoidal_to_xyz;
2909 s->out_transform = fisheye_to_xyz;
2910 prepare_out = prepare_fisheye_out;
2911 w = roundf(wf * 0.5f);
2915 s->out_transform = pannini_to_xyz;
2921 s->out_transform = cylindrical_to_xyz;
2922 prepare_out = prepare_cylindrical_out;
2924 h = roundf(hf * 0.5f);
2927 av_log(ctx, AV_LOG_ERROR, "Specified output format is not handled.\n");
2931 // Override resolution with user values if specified
2932 if (s->width > 0 && s->height > 0) {
2935 } else if (s->width > 0 || s->height > 0) {
2936 av_log(ctx, AV_LOG_ERROR, "Both width and height values should be specified.\n");
2937 return AVERROR(EINVAL);
2939 if (s->out_transpose)
2942 if (s->in_transpose)
2947 fov_from_dfov(s, w, h);
2950 err = prepare_out(ctx);
2955 set_dimensions(s->pr_width, s->pr_height, w, h, desc);
2957 s->out_width = s->pr_width[0];
2958 s->out_height = s->pr_height[0];
2960 if (s->out_transpose)
2961 FFSWAP(int, s->out_width, s->out_height);
2963 switch (s->out_stereo) {
2965 out_offset_w = out_offset_h = 0;
2981 set_dimensions(s->out_offset_w, s->out_offset_h, out_offset_w, out_offset_h, desc);
2982 set_dimensions(s->planewidth, s->planeheight, w, h, desc);
2984 for (int i = 0; i < 4; i++)
2985 s->uv_linesize[i] = FFALIGN(s->pr_width[i], 8);
2990 s->nb_planes = av_pix_fmt_count_planes(inlink->format);
2992 if (desc->log2_chroma_h == desc->log2_chroma_w && desc->log2_chroma_h == 0) {
2993 s->nb_allocated = 1;
2994 s->map[0] = s->map[1] = s->map[2] = s->map[3] = 0;
2996 s->nb_allocated = 2;
2998 s->map[1] = s->map[2] = 1;
3002 for (int i = 0; i < s->nb_allocated; i++)
3003 allocate_plane(s, sizeof_uv, sizeof_ker, i);
3005 calculate_rotation_matrix(s->yaw, s->pitch, s->roll, s->rot_mat, s->rotation_order);
3006 set_mirror_modifier(s->h_flip, s->v_flip, s->d_flip, s->output_mirror_modifier);
3008 ctx->internal->execute(ctx, v360_slice, NULL, NULL, FFMIN(outlink->h, ff_filter_get_nb_threads(ctx)));
3013 static int filter_frame(AVFilterLink *inlink, AVFrame *in)
3015 AVFilterContext *ctx = inlink->dst;
3016 AVFilterLink *outlink = ctx->outputs[0];
3017 V360Context *s = ctx->priv;
3021 out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
3024 return AVERROR(ENOMEM);
3026 av_frame_copy_props(out, in);
3031 ctx->internal->execute(ctx, s->remap_slice, &td, NULL, FFMIN(outlink->h, ff_filter_get_nb_threads(ctx)));
3034 return ff_filter_frame(outlink, out);
3037 static av_cold void uninit(AVFilterContext *ctx)
3039 V360Context *s = ctx->priv;
3041 for (int p = 0; p < s->nb_allocated; p++) {
3044 av_freep(&s->ker[p]);
3048 static const AVFilterPad inputs[] = {
3051 .type = AVMEDIA_TYPE_VIDEO,
3052 .filter_frame = filter_frame,
3057 static const AVFilterPad outputs[] = {
3060 .type = AVMEDIA_TYPE_VIDEO,
3061 .config_props = config_output,
3066 AVFilter ff_vf_v360 = {
3068 .description = NULL_IF_CONFIG_SMALL("Convert 360 projection of video."),
3069 .priv_size = sizeof(V360Context),
3071 .query_formats = query_formats,
3074 .priv_class = &v360_class,
3075 .flags = AVFILTER_FLAG_SLICE_THREADS,