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 { "interp", "set interpolation method", OFFSET(interp), AV_OPT_TYPE_INT, {.i64=BILINEAR}, 0, NB_INTERP_METHODS-1, FLAGS, "interp" },
93 { "near", "nearest neighbour", 0, AV_OPT_TYPE_CONST, {.i64=NEAREST}, 0, 0, FLAGS, "interp" },
94 { "nearest", "nearest neighbour", 0, AV_OPT_TYPE_CONST, {.i64=NEAREST}, 0, 0, FLAGS, "interp" },
95 { "line", "bilinear interpolation", 0, AV_OPT_TYPE_CONST, {.i64=BILINEAR}, 0, 0, FLAGS, "interp" },
96 { "linear", "bilinear interpolation", 0, AV_OPT_TYPE_CONST, {.i64=BILINEAR}, 0, 0, FLAGS, "interp" },
97 { "cube", "bicubic interpolation", 0, AV_OPT_TYPE_CONST, {.i64=BICUBIC}, 0, 0, FLAGS, "interp" },
98 { "cubic", "bicubic interpolation", 0, AV_OPT_TYPE_CONST, {.i64=BICUBIC}, 0, 0, FLAGS, "interp" },
99 { "lanc", "lanczos interpolation", 0, AV_OPT_TYPE_CONST, {.i64=LANCZOS}, 0, 0, FLAGS, "interp" },
100 { "lanczos", "lanczos interpolation", 0, AV_OPT_TYPE_CONST, {.i64=LANCZOS}, 0, 0, FLAGS, "interp" },
101 { "w", "output width", OFFSET(width), AV_OPT_TYPE_INT, {.i64=0}, 0, INT16_MAX, FLAGS, "w"},
102 { "h", "output height", OFFSET(height), AV_OPT_TYPE_INT, {.i64=0}, 0, INT16_MAX, FLAGS, "h"},
103 { "in_stereo", "input stereo format", OFFSET(in_stereo), AV_OPT_TYPE_INT, {.i64=STEREO_2D}, 0, NB_STEREO_FMTS-1, FLAGS, "stereo" },
104 {"out_stereo", "output stereo format", OFFSET(out_stereo), AV_OPT_TYPE_INT, {.i64=STEREO_2D}, 0, NB_STEREO_FMTS-1, FLAGS, "stereo" },
105 { "2d", "2d mono", 0, AV_OPT_TYPE_CONST, {.i64=STEREO_2D}, 0, 0, FLAGS, "stereo" },
106 { "sbs", "side by side", 0, AV_OPT_TYPE_CONST, {.i64=STEREO_SBS}, 0, 0, FLAGS, "stereo" },
107 { "tb", "top bottom", 0, AV_OPT_TYPE_CONST, {.i64=STEREO_TB}, 0, 0, FLAGS, "stereo" },
108 { "in_forder", "input cubemap face order", OFFSET(in_forder), AV_OPT_TYPE_STRING, {.str="rludfb"}, 0, NB_DIRECTIONS-1, FLAGS, "in_forder"},
109 {"out_forder", "output cubemap face order", OFFSET(out_forder), AV_OPT_TYPE_STRING, {.str="rludfb"}, 0, NB_DIRECTIONS-1, FLAGS, "out_forder"},
110 { "in_frot", "input cubemap face rotation", OFFSET(in_frot), AV_OPT_TYPE_STRING, {.str="000000"}, 0, NB_DIRECTIONS-1, FLAGS, "in_frot"},
111 { "out_frot", "output cubemap face rotation",OFFSET(out_frot), AV_OPT_TYPE_STRING, {.str="000000"}, 0, NB_DIRECTIONS-1, FLAGS, "out_frot"},
112 { "in_pad", "percent input cubemap pads", OFFSET(in_pad), AV_OPT_TYPE_FLOAT, {.dbl=0.f}, 0.f, 1.f, FLAGS, "in_pad"},
113 { "out_pad", "percent output cubemap pads", OFFSET(out_pad), AV_OPT_TYPE_FLOAT, {.dbl=0.f}, 0.f, 1.f, FLAGS, "out_pad"},
114 { "fin_pad", "fixed input cubemap pads", OFFSET(fin_pad), AV_OPT_TYPE_INT, {.i64=0}, 0, 100, FLAGS, "fin_pad"},
115 { "fout_pad", "fixed output cubemap pads", OFFSET(fout_pad), AV_OPT_TYPE_INT, {.i64=0}, 0, 100, FLAGS, "fout_pad"},
116 { "yaw", "yaw rotation", OFFSET(yaw), AV_OPT_TYPE_FLOAT, {.dbl=0.f}, -180.f, 180.f, FLAGS, "yaw"},
117 { "pitch", "pitch rotation", OFFSET(pitch), AV_OPT_TYPE_FLOAT, {.dbl=0.f}, -180.f, 180.f, FLAGS, "pitch"},
118 { "roll", "roll rotation", OFFSET(roll), AV_OPT_TYPE_FLOAT, {.dbl=0.f}, -180.f, 180.f, FLAGS, "roll"},
119 { "rorder", "rotation order", OFFSET(rorder), AV_OPT_TYPE_STRING, {.str="ypr"}, 0, 0, FLAGS, "rorder"},
120 { "h_fov", "horizontal field of view", OFFSET(h_fov), AV_OPT_TYPE_FLOAT, {.dbl=90.f}, 0.00001f, 360.f, FLAGS, "h_fov"},
121 { "v_fov", "vertical field of view", OFFSET(v_fov), AV_OPT_TYPE_FLOAT, {.dbl=45.f}, 0.00001f, 360.f, FLAGS, "v_fov"},
122 { "d_fov", "diagonal field of view", OFFSET(d_fov), AV_OPT_TYPE_FLOAT, {.dbl=0.f}, 0.f, 360.f, FLAGS, "d_fov"},
123 { "h_flip", "flip out video horizontally", OFFSET(h_flip), AV_OPT_TYPE_BOOL, {.i64=0}, 0, 1, FLAGS, "h_flip"},
124 { "v_flip", "flip out video vertically", OFFSET(v_flip), AV_OPT_TYPE_BOOL, {.i64=0}, 0, 1, FLAGS, "v_flip"},
125 { "d_flip", "flip out video indepth", OFFSET(d_flip), AV_OPT_TYPE_BOOL, {.i64=0}, 0, 1, FLAGS, "d_flip"},
126 { "ih_flip", "flip in video horizontally", OFFSET(ih_flip), AV_OPT_TYPE_BOOL, {.i64=0}, 0, 1, FLAGS, "ih_flip"},
127 { "iv_flip", "flip in video vertically", OFFSET(iv_flip), AV_OPT_TYPE_BOOL, {.i64=0}, 0, 1, FLAGS, "iv_flip"},
128 { "in_trans", "transpose video input", OFFSET(in_transpose), AV_OPT_TYPE_BOOL, {.i64=0}, 0, 1, FLAGS, "in_transpose"},
129 { "out_trans", "transpose video output", OFFSET(out_transpose), AV_OPT_TYPE_BOOL, {.i64=0}, 0, 1, FLAGS, "out_transpose"},
133 AVFILTER_DEFINE_CLASS(v360);
135 static int query_formats(AVFilterContext *ctx)
137 static const enum AVPixelFormat pix_fmts[] = {
139 AV_PIX_FMT_YUVA444P, AV_PIX_FMT_YUVA444P9,
140 AV_PIX_FMT_YUVA444P10, AV_PIX_FMT_YUVA444P12,
141 AV_PIX_FMT_YUVA444P16,
144 AV_PIX_FMT_YUVA422P, AV_PIX_FMT_YUVA422P9,
145 AV_PIX_FMT_YUVA422P10, AV_PIX_FMT_YUVA422P12,
146 AV_PIX_FMT_YUVA422P16,
149 AV_PIX_FMT_YUVA420P, AV_PIX_FMT_YUVA420P9,
150 AV_PIX_FMT_YUVA420P10, AV_PIX_FMT_YUVA420P16,
153 AV_PIX_FMT_YUVJ444P, AV_PIX_FMT_YUVJ440P,
154 AV_PIX_FMT_YUVJ422P, AV_PIX_FMT_YUVJ420P,
158 AV_PIX_FMT_YUV444P, AV_PIX_FMT_YUV444P9,
159 AV_PIX_FMT_YUV444P10, AV_PIX_FMT_YUV444P12,
160 AV_PIX_FMT_YUV444P14, AV_PIX_FMT_YUV444P16,
163 AV_PIX_FMT_YUV440P, AV_PIX_FMT_YUV440P10,
164 AV_PIX_FMT_YUV440P12,
167 AV_PIX_FMT_YUV422P, AV_PIX_FMT_YUV422P9,
168 AV_PIX_FMT_YUV422P10, AV_PIX_FMT_YUV422P12,
169 AV_PIX_FMT_YUV422P14, AV_PIX_FMT_YUV422P16,
172 AV_PIX_FMT_YUV420P, AV_PIX_FMT_YUV420P9,
173 AV_PIX_FMT_YUV420P10, AV_PIX_FMT_YUV420P12,
174 AV_PIX_FMT_YUV420P14, AV_PIX_FMT_YUV420P16,
183 AV_PIX_FMT_GBRP, AV_PIX_FMT_GBRP9,
184 AV_PIX_FMT_GBRP10, AV_PIX_FMT_GBRP12,
185 AV_PIX_FMT_GBRP14, AV_PIX_FMT_GBRP16,
188 AV_PIX_FMT_GBRAP, AV_PIX_FMT_GBRAP10,
189 AV_PIX_FMT_GBRAP12, AV_PIX_FMT_GBRAP16,
192 AV_PIX_FMT_GRAY8, AV_PIX_FMT_GRAY9,
193 AV_PIX_FMT_GRAY10, AV_PIX_FMT_GRAY12,
194 AV_PIX_FMT_GRAY14, AV_PIX_FMT_GRAY16,
199 AVFilterFormats *fmts_list = ff_make_format_list(pix_fmts);
201 return AVERROR(ENOMEM);
202 return ff_set_common_formats(ctx, fmts_list);
205 #define DEFINE_REMAP1_LINE(bits, div) \
206 static void remap1_##bits##bit_line_c(uint8_t *dst, int width, const uint8_t *src, \
207 ptrdiff_t in_linesize, \
208 const uint16_t *u, const uint16_t *v, const int16_t *ker) \
210 const uint##bits##_t *s = (const uint##bits##_t *)src; \
211 uint##bits##_t *d = (uint##bits##_t *)dst; \
213 in_linesize /= div; \
215 for (int x = 0; x < width; x++) \
216 d[x] = s[v[x] * in_linesize + u[x]]; \
219 DEFINE_REMAP1_LINE( 8, 1)
220 DEFINE_REMAP1_LINE(16, 2)
223 * Generate remapping function with a given window size and pixel depth.
225 * @param ws size of interpolation window
226 * @param bits number of bits per pixel
228 #define DEFINE_REMAP(ws, bits) \
229 static int remap##ws##_##bits##bit_slice(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs) \
231 ThreadData *td = arg; \
232 const V360Context *s = ctx->priv; \
233 const AVFrame *in = td->in; \
234 AVFrame *out = td->out; \
236 for (int stereo = 0; stereo < 1 + s->out_stereo > STEREO_2D; stereo++) { \
237 for (int plane = 0; plane < s->nb_planes; plane++) { \
238 const int in_linesize = in->linesize[plane]; \
239 const int out_linesize = out->linesize[plane]; \
240 const int uv_linesize = s->uv_linesize[plane]; \
241 const int in_offset_w = stereo ? s->in_offset_w[plane] : 0; \
242 const int in_offset_h = stereo ? s->in_offset_h[plane] : 0; \
243 const int out_offset_w = stereo ? s->out_offset_w[plane] : 0; \
244 const int out_offset_h = stereo ? s->out_offset_h[plane] : 0; \
245 const uint8_t *src = in->data[plane] + in_offset_h * in_linesize + in_offset_w * (bits >> 3); \
246 uint8_t *dst = out->data[plane] + out_offset_h * out_linesize + out_offset_w * (bits >> 3); \
247 const int width = s->pr_width[plane]; \
248 const int height = s->pr_height[plane]; \
250 const int slice_start = (height * jobnr ) / nb_jobs; \
251 const int slice_end = (height * (jobnr + 1)) / nb_jobs; \
253 for (int y = slice_start; y < slice_end; y++) { \
254 const unsigned map = s->map[plane]; \
255 const uint16_t *u = s->u[map] + y * uv_linesize * ws * ws; \
256 const uint16_t *v = s->v[map] + y * uv_linesize * ws * ws; \
257 const int16_t *ker = s->ker[map] + y * uv_linesize * ws * ws; \
259 s->remap_line(dst + y * out_linesize, width, src, in_linesize, u, v, ker); \
274 #define DEFINE_REMAP_LINE(ws, bits, div) \
275 static void remap##ws##_##bits##bit_line_c(uint8_t *dst, int width, const uint8_t *src, \
276 ptrdiff_t in_linesize, \
277 const uint16_t *u, const uint16_t *v, const int16_t *ker) \
279 const uint##bits##_t *s = (const uint##bits##_t *)src; \
280 uint##bits##_t *d = (uint##bits##_t *)dst; \
282 in_linesize /= div; \
284 for (int x = 0; x < width; x++) { \
285 const uint16_t *uu = u + x * ws * ws; \
286 const uint16_t *vv = v + x * ws * ws; \
287 const int16_t *kker = ker + x * ws * ws; \
290 for (int i = 0; i < ws; i++) { \
291 for (int j = 0; j < ws; j++) { \
292 tmp += kker[i * ws + j] * s[vv[i * ws + j] * in_linesize + uu[i * ws + j]]; \
296 d[x] = av_clip_uint##bits(tmp >> 14); \
300 DEFINE_REMAP_LINE(2, 8, 1)
301 DEFINE_REMAP_LINE(4, 8, 1)
302 DEFINE_REMAP_LINE(2, 16, 2)
303 DEFINE_REMAP_LINE(4, 16, 2)
305 void ff_v360_init(V360Context *s, int depth)
309 s->remap_line = depth <= 8 ? remap1_8bit_line_c : remap1_16bit_line_c;
312 s->remap_line = depth <= 8 ? remap2_8bit_line_c : remap2_16bit_line_c;
316 s->remap_line = depth <= 8 ? remap4_8bit_line_c : remap4_16bit_line_c;
321 ff_v360_init_x86(s, depth);
325 * Save nearest pixel coordinates for remapping.
327 * @param du horizontal relative coordinate
328 * @param dv vertical relative coordinate
329 * @param rmap calculated 4x4 window
330 * @param u u remap data
331 * @param v v remap data
332 * @param ker ker remap data
334 static void nearest_kernel(float du, float dv, const XYRemap *rmap,
335 uint16_t *u, uint16_t *v, int16_t *ker)
337 const int i = roundf(dv) + 1;
338 const int j = roundf(du) + 1;
340 u[0] = rmap->u[i][j];
341 v[0] = rmap->v[i][j];
345 * Calculate kernel for bilinear interpolation.
347 * @param du horizontal relative coordinate
348 * @param dv vertical relative coordinate
349 * @param rmap calculated 4x4 window
350 * @param u u remap data
351 * @param v v remap data
352 * @param ker ker remap data
354 static void bilinear_kernel(float du, float dv, const XYRemap *rmap,
355 uint16_t *u, uint16_t *v, int16_t *ker)
357 for (int i = 0; i < 2; i++) {
358 for (int j = 0; j < 2; j++) {
359 u[i * 2 + j] = rmap->u[i + 1][j + 1];
360 v[i * 2 + j] = rmap->v[i + 1][j + 1];
364 ker[0] = lrintf((1.f - du) * (1.f - dv) * 16385.f);
365 ker[1] = lrintf( du * (1.f - dv) * 16385.f);
366 ker[2] = lrintf((1.f - du) * dv * 16385.f);
367 ker[3] = lrintf( du * dv * 16385.f);
371 * Calculate 1-dimensional cubic coefficients.
373 * @param t relative coordinate
374 * @param coeffs coefficients
376 static inline void calculate_bicubic_coeffs(float t, float *coeffs)
378 const float tt = t * t;
379 const float ttt = t * t * t;
381 coeffs[0] = - t / 3.f + tt / 2.f - ttt / 6.f;
382 coeffs[1] = 1.f - t / 2.f - tt + ttt / 2.f;
383 coeffs[2] = t + tt / 2.f - ttt / 2.f;
384 coeffs[3] = - t / 6.f + ttt / 6.f;
388 * Calculate kernel for bicubic interpolation.
390 * @param du horizontal relative coordinate
391 * @param dv vertical relative coordinate
392 * @param rmap calculated 4x4 window
393 * @param u u remap data
394 * @param v v remap data
395 * @param ker ker remap data
397 static void bicubic_kernel(float du, float dv, const XYRemap *rmap,
398 uint16_t *u, uint16_t *v, int16_t *ker)
403 calculate_bicubic_coeffs(du, du_coeffs);
404 calculate_bicubic_coeffs(dv, dv_coeffs);
406 for (int i = 0; i < 4; i++) {
407 for (int j = 0; j < 4; j++) {
408 u[i * 4 + j] = rmap->u[i][j];
409 v[i * 4 + j] = rmap->v[i][j];
410 ker[i * 4 + j] = lrintf(du_coeffs[j] * dv_coeffs[i] * 16385.f);
416 * Calculate 1-dimensional lanczos coefficients.
418 * @param t relative coordinate
419 * @param coeffs coefficients
421 static inline void calculate_lanczos_coeffs(float t, float *coeffs)
425 for (int i = 0; i < 4; i++) {
426 const float x = M_PI * (t - i + 1);
430 coeffs[i] = sinf(x) * sinf(x / 2.f) / (x * x / 2.f);
435 for (int i = 0; i < 4; i++) {
441 * Calculate kernel for lanczos interpolation.
443 * @param du horizontal relative coordinate
444 * @param dv vertical relative coordinate
445 * @param rmap calculated 4x4 window
446 * @param u u remap data
447 * @param v v remap data
448 * @param ker ker remap data
450 static void lanczos_kernel(float du, float dv, const XYRemap *rmap,
451 uint16_t *u, uint16_t *v, int16_t *ker)
456 calculate_lanczos_coeffs(du, du_coeffs);
457 calculate_lanczos_coeffs(dv, dv_coeffs);
459 for (int i = 0; i < 4; i++) {
460 for (int j = 0; j < 4; j++) {
461 u[i * 4 + j] = rmap->u[i][j];
462 v[i * 4 + j] = rmap->v[i][j];
463 ker[i * 4 + j] = lrintf(du_coeffs[j] * dv_coeffs[i] * 16385.f);
469 * Modulo operation with only positive remainders.
474 * @return positive remainder of (a / b)
476 static inline int mod(int a, int b)
478 const int res = a % b;
487 * Convert char to corresponding direction.
488 * Used for cubemap options.
490 static int get_direction(char c)
511 * Convert char to corresponding rotation angle.
512 * Used for cubemap options.
514 static int get_rotation(char c)
531 * Convert char to corresponding rotation order.
533 static int get_rorder(char c)
551 * Prepare data for processing cubemap input format.
553 * @param ctx filter context
557 static int prepare_cube_in(AVFilterContext *ctx)
559 V360Context *s = ctx->priv;
561 for (int face = 0; face < NB_FACES; face++) {
562 const char c = s->in_forder[face];
566 av_log(ctx, AV_LOG_ERROR,
567 "Incomplete in_forder option. Direction for all 6 faces should be specified.\n");
568 return AVERROR(EINVAL);
571 direction = get_direction(c);
572 if (direction == -1) {
573 av_log(ctx, AV_LOG_ERROR,
574 "Incorrect direction symbol '%c' in in_forder option.\n", c);
575 return AVERROR(EINVAL);
578 s->in_cubemap_face_order[direction] = face;
581 for (int face = 0; face < NB_FACES; face++) {
582 const char c = s->in_frot[face];
586 av_log(ctx, AV_LOG_ERROR,
587 "Incomplete in_frot option. Rotation for all 6 faces should be specified.\n");
588 return AVERROR(EINVAL);
591 rotation = get_rotation(c);
592 if (rotation == -1) {
593 av_log(ctx, AV_LOG_ERROR,
594 "Incorrect rotation symbol '%c' in in_frot option.\n", c);
595 return AVERROR(EINVAL);
598 s->in_cubemap_face_rotation[face] = rotation;
605 * Prepare data for processing cubemap output format.
607 * @param ctx filter context
611 static int prepare_cube_out(AVFilterContext *ctx)
613 V360Context *s = ctx->priv;
615 for (int face = 0; face < NB_FACES; face++) {
616 const char c = s->out_forder[face];
620 av_log(ctx, AV_LOG_ERROR,
621 "Incomplete out_forder option. Direction for all 6 faces should be specified.\n");
622 return AVERROR(EINVAL);
625 direction = get_direction(c);
626 if (direction == -1) {
627 av_log(ctx, AV_LOG_ERROR,
628 "Incorrect direction symbol '%c' in out_forder option.\n", c);
629 return AVERROR(EINVAL);
632 s->out_cubemap_direction_order[face] = direction;
635 for (int face = 0; face < NB_FACES; face++) {
636 const char c = s->out_frot[face];
640 av_log(ctx, AV_LOG_ERROR,
641 "Incomplete out_frot option. Rotation for all 6 faces should be specified.\n");
642 return AVERROR(EINVAL);
645 rotation = get_rotation(c);
646 if (rotation == -1) {
647 av_log(ctx, AV_LOG_ERROR,
648 "Incorrect rotation symbol '%c' in out_frot option.\n", c);
649 return AVERROR(EINVAL);
652 s->out_cubemap_face_rotation[face] = rotation;
658 static inline void rotate_cube_face(float *uf, float *vf, int rotation)
684 static inline void rotate_cube_face_inverse(float *uf, float *vf, int rotation)
715 static void normalize_vector(float *vec)
717 const float norm = sqrtf(vec[0] * vec[0] + vec[1] * vec[1] + vec[2] * vec[2]);
725 * Calculate 3D coordinates on sphere for corresponding cubemap position.
726 * Common operation for every cubemap.
728 * @param s filter private context
729 * @param uf horizontal cubemap coordinate [0, 1)
730 * @param vf vertical cubemap coordinate [0, 1)
731 * @param face face of cubemap
732 * @param vec coordinates on sphere
733 * @param scalew scale for uf
734 * @param scaleh scale for vf
736 static void cube_to_xyz(const V360Context *s,
737 float uf, float vf, int face,
738 float *vec, float scalew, float scaleh)
740 const int direction = s->out_cubemap_direction_order[face];
746 rotate_cube_face_inverse(&uf, &vf, s->out_cubemap_face_rotation[face]);
787 normalize_vector(vec);
791 * Calculate cubemap position for corresponding 3D coordinates on sphere.
792 * Common operation for every cubemap.
794 * @param s filter private context
795 * @param vec coordinated on sphere
796 * @param uf horizontal cubemap coordinate [0, 1)
797 * @param vf vertical cubemap coordinate [0, 1)
798 * @param direction direction of view
800 static void xyz_to_cube(const V360Context *s,
802 float *uf, float *vf, int *direction)
804 const float phi = atan2f(vec[0], -vec[2]);
805 const float theta = asinf(-vec[1]);
806 float phi_norm, theta_threshold;
809 if (phi >= -M_PI_4 && phi < M_PI_4) {
812 } else if (phi >= -(M_PI_2 + M_PI_4) && phi < -M_PI_4) {
814 phi_norm = phi + M_PI_2;
815 } else if (phi >= M_PI_4 && phi < M_PI_2 + M_PI_4) {
817 phi_norm = phi - M_PI_2;
820 phi_norm = phi + ((phi > 0.f) ? -M_PI : M_PI);
823 theta_threshold = atanf(cosf(phi_norm));
824 if (theta > theta_threshold) {
826 } else if (theta < -theta_threshold) {
830 switch (*direction) {
832 *uf = vec[2] / vec[0];
833 *vf = -vec[1] / vec[0];
836 *uf = vec[2] / vec[0];
837 *vf = vec[1] / vec[0];
840 *uf = vec[0] / vec[1];
841 *vf = -vec[2] / vec[1];
844 *uf = -vec[0] / vec[1];
845 *vf = -vec[2] / vec[1];
848 *uf = -vec[0] / vec[2];
849 *vf = vec[1] / vec[2];
852 *uf = -vec[0] / vec[2];
853 *vf = -vec[1] / vec[2];
859 face = s->in_cubemap_face_order[*direction];
860 rotate_cube_face(uf, vf, s->in_cubemap_face_rotation[face]);
862 (*uf) *= s->input_mirror_modifier[0];
863 (*vf) *= s->input_mirror_modifier[1];
867 * Find position on another cube face in case of overflow/underflow.
868 * Used for calculation of interpolation window.
870 * @param s filter private context
871 * @param uf horizontal cubemap coordinate
872 * @param vf vertical cubemap coordinate
873 * @param direction direction of view
874 * @param new_uf new horizontal cubemap coordinate
875 * @param new_vf new vertical cubemap coordinate
876 * @param face face position on cubemap
878 static void process_cube_coordinates(const V360Context *s,
879 float uf, float vf, int direction,
880 float *new_uf, float *new_vf, int *face)
883 * Cubemap orientation
890 * +-------+-------+-------+-------+ ^ e |
892 * | left | front | right | back | | g |
893 * +-------+-------+-------+-------+ v h v
899 *face = s->in_cubemap_face_order[direction];
900 rotate_cube_face_inverse(&uf, &vf, s->in_cubemap_face_rotation[*face]);
902 if ((uf < -1.f || uf >= 1.f) && (vf < -1.f || vf >= 1.f)) {
903 // There are no pixels to use in this case
906 } else if (uf < -1.f) {
942 } else if (uf >= 1.f) {
978 } else if (vf < -1.f) {
1014 } else if (vf >= 1.f) {
1016 switch (direction) {
1056 *face = s->in_cubemap_face_order[direction];
1057 rotate_cube_face(new_uf, new_vf, s->in_cubemap_face_rotation[*face]);
1061 * Calculate 3D coordinates on sphere for corresponding frame position in cubemap3x2 format.
1063 * @param s filter private context
1064 * @param i horizontal position on frame [0, width)
1065 * @param j vertical position on frame [0, height)
1066 * @param width frame width
1067 * @param height frame height
1068 * @param vec coordinates on sphere
1070 static void cube3x2_to_xyz(const V360Context *s,
1071 int i, int j, int width, int height,
1074 const float scalew = s->fout_pad > 0 ? 1.f - s->fout_pad / (s->out_width / 3.f) : 1.f - s->out_pad;
1075 const float scaleh = s->fout_pad > 0 ? 1.f - s->fout_pad / (s->out_height / 2.f) : 1.f - s->out_pad;
1077 const float ew = width / 3.f;
1078 const float eh = height / 2.f;
1080 const int u_face = floorf(i / ew);
1081 const int v_face = floorf(j / eh);
1082 const int face = u_face + 3 * v_face;
1084 const int u_shift = ceilf(ew * u_face);
1085 const int v_shift = ceilf(eh * v_face);
1086 const int ewi = ceilf(ew * (u_face + 1)) - u_shift;
1087 const int ehi = ceilf(eh * (v_face + 1)) - v_shift;
1089 const float uf = 2.f * (i - u_shift + 0.5f) / ewi - 1.f;
1090 const float vf = 2.f * (j - v_shift + 0.5f) / ehi - 1.f;
1092 cube_to_xyz(s, uf, vf, face, vec, scalew, scaleh);
1096 * Calculate frame position in cubemap3x2 format for corresponding 3D coordinates on sphere.
1098 * @param s filter private context
1099 * @param vec coordinates on sphere
1100 * @param width frame width
1101 * @param height frame height
1102 * @param us horizontal coordinates for interpolation window
1103 * @param vs vertical coordinates for interpolation window
1104 * @param du horizontal relative coordinate
1105 * @param dv vertical relative coordinate
1107 static void xyz_to_cube3x2(const V360Context *s,
1108 const float *vec, int width, int height,
1109 uint16_t us[4][4], uint16_t vs[4][4], float *du, float *dv)
1111 const float scalew = s->fin_pad > 0 ? 1.f - s->fin_pad / (s->in_width / 3.f) : 1.f - s->in_pad;
1112 const float scaleh = s->fin_pad > 0 ? 1.f - s->fin_pad / (s->in_height / 2.f) : 1.f - s->in_pad;
1113 const float ew = width / 3.f;
1114 const float eh = height / 2.f;
1118 int direction, face;
1121 xyz_to_cube(s, vec, &uf, &vf, &direction);
1126 face = s->in_cubemap_face_order[direction];
1129 ewi = ceilf(ew * (u_face + 1)) - ceilf(ew * u_face);
1130 ehi = ceilf(eh * (v_face + 1)) - ceilf(eh * v_face);
1132 uf = 0.5f * ewi * (uf + 1.f) - 0.5f;
1133 vf = 0.5f * ehi * (vf + 1.f) - 0.5f;
1141 for (int i = -1; i < 3; i++) {
1142 for (int j = -1; j < 3; j++) {
1143 int new_ui = ui + j;
1144 int new_vi = vi + i;
1145 int u_shift, v_shift;
1146 int new_ewi, new_ehi;
1148 if (new_ui >= 0 && new_ui < ewi && new_vi >= 0 && new_vi < ehi) {
1149 face = s->in_cubemap_face_order[direction];
1153 u_shift = ceilf(ew * u_face);
1154 v_shift = ceilf(eh * v_face);
1156 uf = 2.f * new_ui / ewi - 1.f;
1157 vf = 2.f * new_vi / ehi - 1.f;
1162 process_cube_coordinates(s, uf, vf, direction, &uf, &vf, &face);
1169 u_shift = ceilf(ew * u_face);
1170 v_shift = ceilf(eh * v_face);
1171 new_ewi = ceilf(ew * (u_face + 1)) - u_shift;
1172 new_ehi = ceilf(eh * (v_face + 1)) - v_shift;
1174 new_ui = av_clip(roundf(0.5f * new_ewi * (uf + 1.f)), 0, new_ewi - 1);
1175 new_vi = av_clip(roundf(0.5f * new_ehi * (vf + 1.f)), 0, new_ehi - 1);
1178 us[i + 1][j + 1] = u_shift + new_ui;
1179 vs[i + 1][j + 1] = v_shift + new_vi;
1185 * Calculate 3D coordinates on sphere for corresponding frame position in cubemap1x6 format.
1187 * @param s filter private context
1188 * @param i horizontal position on frame [0, width)
1189 * @param j vertical position on frame [0, height)
1190 * @param width frame width
1191 * @param height frame height
1192 * @param vec coordinates on sphere
1194 static void cube1x6_to_xyz(const V360Context *s,
1195 int i, int j, int width, int height,
1198 const float scalew = s->fout_pad > 0 ? 1.f - (float)(s->fout_pad) / s->out_width : 1.f - s->out_pad;
1199 const float scaleh = s->fout_pad > 0 ? 1.f - s->fout_pad / (s->out_height / 6.f) : 1.f - s->out_pad;
1201 const float ew = width;
1202 const float eh = height / 6.f;
1204 const int face = floorf(j / eh);
1206 const int v_shift = ceilf(eh * face);
1207 const int ehi = ceilf(eh * (face + 1)) - v_shift;
1209 const float uf = 2.f * (i + 0.5f) / ew - 1.f;
1210 const float vf = 2.f * (j - v_shift + 0.5f) / ehi - 1.f;
1212 cube_to_xyz(s, uf, vf, face, vec, scalew, scaleh);
1216 * Calculate 3D coordinates on sphere for corresponding frame position in cubemap6x1 format.
1218 * @param s filter private context
1219 * @param i horizontal position on frame [0, width)
1220 * @param j vertical position on frame [0, height)
1221 * @param width frame width
1222 * @param height frame height
1223 * @param vec coordinates on sphere
1225 static void cube6x1_to_xyz(const V360Context *s,
1226 int i, int j, int width, int height,
1229 const float scalew = s->fout_pad > 0 ? 1.f - s->fout_pad / (s->out_width / 6.f) : 1.f - s->out_pad;
1230 const float scaleh = s->fout_pad > 0 ? 1.f - (float)(s->fout_pad) / s->out_height : 1.f - s->out_pad;
1232 const float ew = width / 6.f;
1233 const float eh = height;
1235 const int face = floorf(i / ew);
1237 const int u_shift = ceilf(ew * face);
1238 const int ewi = ceilf(ew * (face + 1)) - u_shift;
1240 const float uf = 2.f * (i - u_shift + 0.5f) / ewi - 1.f;
1241 const float vf = 2.f * (j + 0.5f) / eh - 1.f;
1243 cube_to_xyz(s, uf, vf, face, vec, scalew, scaleh);
1247 * Calculate frame position in cubemap1x6 format for corresponding 3D coordinates on sphere.
1249 * @param s filter private context
1250 * @param vec coordinates on sphere
1251 * @param width frame width
1252 * @param height frame height
1253 * @param us horizontal coordinates for interpolation window
1254 * @param vs vertical coordinates for interpolation window
1255 * @param du horizontal relative coordinate
1256 * @param dv vertical relative coordinate
1258 static void xyz_to_cube1x6(const V360Context *s,
1259 const float *vec, int width, int height,
1260 uint16_t us[4][4], uint16_t vs[4][4], float *du, float *dv)
1262 const float scalew = s->fin_pad > 0 ? 1.f - (float)(s->fin_pad) / s->in_width : 1.f - s->in_pad;
1263 const float scaleh = s->fin_pad > 0 ? 1.f - s->fin_pad / (s->in_height / 6.f) : 1.f - s->in_pad;
1264 const float eh = height / 6.f;
1265 const int ewi = width;
1269 int direction, face;
1271 xyz_to_cube(s, vec, &uf, &vf, &direction);
1276 face = s->in_cubemap_face_order[direction];
1277 ehi = ceilf(eh * (face + 1)) - ceilf(eh * face);
1279 uf = 0.5f * ewi * (uf + 1.f) - 0.5f;
1280 vf = 0.5f * ehi * (vf + 1.f) - 0.5f;
1288 for (int i = -1; i < 3; i++) {
1289 for (int j = -1; j < 3; j++) {
1290 int new_ui = ui + j;
1291 int new_vi = vi + i;
1295 if (new_ui >= 0 && new_ui < ewi && new_vi >= 0 && new_vi < ehi) {
1296 face = s->in_cubemap_face_order[direction];
1298 v_shift = ceilf(eh * face);
1300 uf = 2.f * new_ui / ewi - 1.f;
1301 vf = 2.f * new_vi / ehi - 1.f;
1306 process_cube_coordinates(s, uf, vf, direction, &uf, &vf, &face);
1311 v_shift = ceilf(eh * face);
1312 new_ehi = ceilf(eh * (face + 1)) - v_shift;
1314 new_ui = av_clip(roundf(0.5f * ewi * (uf + 1.f)), 0, ewi - 1);
1315 new_vi = av_clip(roundf(0.5f * new_ehi * (vf + 1.f)), 0, new_ehi - 1);
1318 us[i + 1][j + 1] = new_ui;
1319 vs[i + 1][j + 1] = v_shift + new_vi;
1325 * Calculate frame position in cubemap6x1 format for corresponding 3D coordinates on sphere.
1327 * @param s filter private context
1328 * @param vec coordinates on sphere
1329 * @param width frame width
1330 * @param height frame height
1331 * @param us horizontal coordinates for interpolation window
1332 * @param vs vertical coordinates for interpolation window
1333 * @param du horizontal relative coordinate
1334 * @param dv vertical relative coordinate
1336 static void xyz_to_cube6x1(const V360Context *s,
1337 const float *vec, int width, int height,
1338 uint16_t us[4][4], uint16_t vs[4][4], float *du, float *dv)
1340 const float scalew = s->fin_pad > 0 ? 1.f - s->fin_pad / (s->in_width / 6.f) : 1.f - s->in_pad;
1341 const float scaleh = s->fin_pad > 0 ? 1.f - (float)(s->fin_pad) / s->in_height : 1.f - s->in_pad;
1342 const float ew = width / 6.f;
1343 const int ehi = height;
1347 int direction, face;
1349 xyz_to_cube(s, vec, &uf, &vf, &direction);
1354 face = s->in_cubemap_face_order[direction];
1355 ewi = ceilf(ew * (face + 1)) - ceilf(ew * face);
1357 uf = 0.5f * ewi * (uf + 1.f) - 0.5f;
1358 vf = 0.5f * ehi * (vf + 1.f) - 0.5f;
1366 for (int i = -1; i < 3; i++) {
1367 for (int j = -1; j < 3; j++) {
1368 int new_ui = ui + j;
1369 int new_vi = vi + i;
1373 if (new_ui >= 0 && new_ui < ewi && new_vi >= 0 && new_vi < ehi) {
1374 face = s->in_cubemap_face_order[direction];
1376 u_shift = ceilf(ew * face);
1378 uf = 2.f * new_ui / ewi - 1.f;
1379 vf = 2.f * new_vi / ehi - 1.f;
1384 process_cube_coordinates(s, uf, vf, direction, &uf, &vf, &face);
1389 u_shift = ceilf(ew * face);
1390 new_ewi = ceilf(ew * (face + 1)) - u_shift;
1392 new_ui = av_clip(roundf(0.5f * new_ewi * (uf + 1.f)), 0, new_ewi - 1);
1393 new_vi = av_clip(roundf(0.5f * ehi * (vf + 1.f)), 0, ehi - 1);
1396 us[i + 1][j + 1] = u_shift + new_ui;
1397 vs[i + 1][j + 1] = new_vi;
1403 * Calculate 3D coordinates on sphere for corresponding frame position in equirectangular format.
1405 * @param s filter private context
1406 * @param i horizontal position on frame [0, width)
1407 * @param j vertical position on frame [0, height)
1408 * @param width frame width
1409 * @param height frame height
1410 * @param vec coordinates on sphere
1412 static void equirect_to_xyz(const V360Context *s,
1413 int i, int j, int width, int height,
1416 const float phi = ((2.f * i) / width - 1.f) * M_PI;
1417 const float theta = ((2.f * j) / height - 1.f) * M_PI_2;
1419 const float sin_phi = sinf(phi);
1420 const float cos_phi = cosf(phi);
1421 const float sin_theta = sinf(theta);
1422 const float cos_theta = cosf(theta);
1424 vec[0] = cos_theta * sin_phi;
1425 vec[1] = -sin_theta;
1426 vec[2] = -cos_theta * cos_phi;
1430 * Prepare data for processing stereographic output format.
1432 * @param ctx filter context
1434 * @return error code
1436 static int prepare_stereographic_out(AVFilterContext *ctx)
1438 V360Context *s = ctx->priv;
1440 s->flat_range[0] = tanf(FFMIN(s->h_fov, 359.f) * M_PI / 720.f);
1441 s->flat_range[1] = tanf(FFMIN(s->v_fov, 359.f) * M_PI / 720.f);
1447 * Calculate 3D coordinates on sphere for corresponding frame position in stereographic format.
1449 * @param s filter private context
1450 * @param i horizontal position on frame [0, width)
1451 * @param j vertical position on frame [0, height)
1452 * @param width frame width
1453 * @param height frame height
1454 * @param vec coordinates on sphere
1456 static void stereographic_to_xyz(const V360Context *s,
1457 int i, int j, int width, int height,
1460 const float x = ((2.f * i) / width - 1.f) * s->flat_range[0];
1461 const float y = ((2.f * j) / height - 1.f) * s->flat_range[1];
1462 const float xy = x * x + y * y;
1464 vec[0] = 2.f * x / (1.f + xy);
1465 vec[1] = (-1.f + xy) / (1.f + xy);
1466 vec[2] = 2.f * y / (1.f + xy);
1468 normalize_vector(vec);
1472 * Calculate frame position in stereographic format for corresponding 3D coordinates on sphere.
1474 * @param s filter private context
1475 * @param vec coordinates on sphere
1476 * @param width frame width
1477 * @param height frame height
1478 * @param us horizontal coordinates for interpolation window
1479 * @param vs vertical coordinates for interpolation window
1480 * @param du horizontal relative coordinate
1481 * @param dv vertical relative coordinate
1483 static void xyz_to_stereographic(const V360Context *s,
1484 const float *vec, int width, int height,
1485 uint16_t us[4][4], uint16_t vs[4][4], float *du, float *dv)
1487 const float x = av_clipf(vec[0] / (1.f - vec[1]), -1.f, 1.f) * s->input_mirror_modifier[0];
1488 const float y = av_clipf(vec[2] / (1.f - vec[1]), -1.f, 1.f) * s->input_mirror_modifier[1];
1492 uf = (x + 1.f) * width / 2.f;
1493 vf = (y + 1.f) * height / 2.f;
1500 for (int i = -1; i < 3; i++) {
1501 for (int j = -1; j < 3; j++) {
1502 us[i + 1][j + 1] = av_clip(ui + j, 0, width - 1);
1503 vs[i + 1][j + 1] = av_clip(vi + i, 0, height - 1);
1509 * Calculate frame position in equirectangular format for corresponding 3D coordinates on sphere.
1511 * @param s filter private context
1512 * @param vec coordinates on sphere
1513 * @param width frame width
1514 * @param height frame height
1515 * @param us horizontal coordinates for interpolation window
1516 * @param vs vertical coordinates for interpolation window
1517 * @param du horizontal relative coordinate
1518 * @param dv vertical relative coordinate
1520 static void xyz_to_equirect(const V360Context *s,
1521 const float *vec, int width, int height,
1522 uint16_t us[4][4], uint16_t vs[4][4], float *du, float *dv)
1524 const float phi = atan2f(vec[0], -vec[2]) * s->input_mirror_modifier[0];
1525 const float theta = asinf(-vec[1]) * s->input_mirror_modifier[1];
1529 uf = (phi / M_PI + 1.f) * width / 2.f;
1530 vf = (theta / M_PI_2 + 1.f) * height / 2.f;
1537 for (int i = -1; i < 3; i++) {
1538 for (int j = -1; j < 3; j++) {
1539 us[i + 1][j + 1] = mod(ui + j, width);
1540 vs[i + 1][j + 1] = av_clip(vi + i, 0, height - 1);
1546 * Calculate frame position in mercator format for corresponding 3D coordinates on sphere.
1548 * @param s filter private context
1549 * @param vec coordinates on sphere
1550 * @param width frame width
1551 * @param height frame height
1552 * @param us horizontal coordinates for interpolation window
1553 * @param vs vertical coordinates for interpolation window
1554 * @param du horizontal relative coordinate
1555 * @param dv vertical relative coordinate
1557 static void xyz_to_mercator(const V360Context *s,
1558 const float *vec, int width, int height,
1559 uint16_t us[4][4], uint16_t vs[4][4], float *du, float *dv)
1561 const float phi = atan2f(vec[0], -vec[2]) * s->input_mirror_modifier[0];
1562 const float theta = -vec[1] * s->input_mirror_modifier[1];
1566 uf = (phi / M_PI + 1.f) * width / 2.f;
1567 vf = (av_clipf(logf((1.f + theta) / (1.f - theta)) / (2.f * M_PI), -1.f, 1.f) + 1.f) * height / 2.f;
1574 for (int i = -1; i < 3; i++) {
1575 for (int j = -1; j < 3; j++) {
1576 us[i + 1][j + 1] = av_clip(ui + j, 0, width - 1);
1577 vs[i + 1][j + 1] = av_clip(vi + i, 0, height - 1);
1583 * Calculate 3D coordinates on sphere for corresponding frame position in mercator format.
1585 * @param s filter private context
1586 * @param i horizontal position on frame [0, width)
1587 * @param j vertical position on frame [0, height)
1588 * @param width frame width
1589 * @param height frame height
1590 * @param vec coordinates on sphere
1592 static void mercator_to_xyz(const V360Context *s,
1593 int i, int j, int width, int height,
1596 const float phi = ((2.f * i) / width - 1.f) * M_PI + M_PI_2;
1597 const float y = ((2.f * j) / height - 1.f) * M_PI;
1598 const float div = expf(2.f * y) + 1.f;
1600 const float sin_phi = sinf(phi);
1601 const float cos_phi = cosf(phi);
1602 const float sin_theta = -2.f * expf(y) / div;
1603 const float cos_theta = -(expf(2.f * y) - 1.f) / div;
1605 vec[0] = sin_theta * cos_phi;
1607 vec[2] = sin_theta * sin_phi;
1611 * Calculate frame position in ball format for corresponding 3D coordinates on sphere.
1613 * @param s filter private context
1614 * @param vec coordinates on sphere
1615 * @param width frame width
1616 * @param height frame height
1617 * @param us horizontal coordinates for interpolation window
1618 * @param vs vertical coordinates for interpolation window
1619 * @param du horizontal relative coordinate
1620 * @param dv vertical relative coordinate
1622 static void xyz_to_ball(const V360Context *s,
1623 const float *vec, int width, int height,
1624 uint16_t us[4][4], uint16_t vs[4][4], float *du, float *dv)
1626 const float l = hypotf(vec[0], vec[1]);
1627 const float r = sqrtf(1.f + vec[2]) / M_SQRT2;
1631 uf = (1.f + r * vec[0] * s->input_mirror_modifier[0] / (l > 0.f ? l : 1.f)) * width * 0.5f;
1632 vf = (1.f - r * vec[1] * s->input_mirror_modifier[1] / (l > 0.f ? l : 1.f)) * height * 0.5f;
1640 for (int i = -1; i < 3; i++) {
1641 for (int j = -1; j < 3; j++) {
1642 us[i + 1][j + 1] = av_clip(ui + j, 0, width - 1);
1643 vs[i + 1][j + 1] = av_clip(vi + i, 0, height - 1);
1649 * Calculate 3D coordinates on sphere for corresponding frame position in ball format.
1651 * @param s filter private context
1652 * @param i horizontal position on frame [0, width)
1653 * @param j vertical position on frame [0, height)
1654 * @param width frame width
1655 * @param height frame height
1656 * @param vec coordinates on sphere
1658 static void ball_to_xyz(const V360Context *s,
1659 int i, int j, int width, int height,
1662 const float x = (2.f * i) / width - 1.f;
1663 const float y = (2.f * j) / height - 1.f;
1664 const float l = hypotf(x, y);
1667 const float z = 2.f * l * sqrtf(1.f - l * l);
1669 vec[0] = z * x / (l > 0.f ? l : 1.f);
1670 vec[1] = -z * y / (l > 0.f ? l : 1.f);
1671 vec[2] = -1.f + 2.f * l * l;
1680 * Calculate 3D coordinates on sphere for corresponding frame position in hammer format.
1682 * @param s filter private context
1683 * @param i horizontal position on frame [0, width)
1684 * @param j vertical position on frame [0, height)
1685 * @param width frame width
1686 * @param height frame height
1687 * @param vec coordinates on sphere
1689 static void hammer_to_xyz(const V360Context *s,
1690 int i, int j, int width, int height,
1693 const float x = ((2.f * i) / width - 1.f);
1694 const float y = ((2.f * j) / height - 1.f);
1696 const float xx = x * x;
1697 const float yy = y * y;
1699 const float z = sqrtf(1.f - xx * 0.5f - yy * 0.5f);
1701 const float a = M_SQRT2 * x * z;
1702 const float b = 2.f * z * z - 1.f;
1704 const float aa = a * a;
1705 const float bb = b * b;
1707 const float w = sqrtf(1.f - 2.f * yy * z * z);
1709 vec[0] = w * 2.f * a * b / (aa + bb);
1710 vec[1] = -M_SQRT2 * y * z;
1711 vec[2] = -w * (bb - aa) / (aa + bb);
1713 normalize_vector(vec);
1717 * Calculate frame position in hammer format for corresponding 3D coordinates on sphere.
1719 * @param s filter private context
1720 * @param vec coordinates on sphere
1721 * @param width frame width
1722 * @param height frame height
1723 * @param us horizontal coordinates for interpolation window
1724 * @param vs vertical coordinates for interpolation window
1725 * @param du horizontal relative coordinate
1726 * @param dv vertical relative coordinate
1728 static void xyz_to_hammer(const V360Context *s,
1729 const float *vec, int width, int height,
1730 uint16_t us[4][4], uint16_t vs[4][4], float *du, float *dv)
1732 const float theta = atan2f(vec[0], -vec[2]) * s->input_mirror_modifier[0];
1734 const float z = sqrtf(1.f + sqrtf(1.f - vec[1] * vec[1]) * cosf(theta * 0.5f));
1735 const float x = sqrtf(1.f - vec[1] * vec[1]) * sinf(theta * 0.5f) / z;
1736 const float y = -vec[1] / z * s->input_mirror_modifier[1];
1740 uf = (x + 1.f) * width / 2.f;
1741 vf = (y + 1.f) * height / 2.f;
1748 for (int i = -1; i < 3; i++) {
1749 for (int j = -1; j < 3; j++) {
1750 us[i + 1][j + 1] = av_clip(ui + j, 0, width - 1);
1751 vs[i + 1][j + 1] = av_clip(vi + i, 0, height - 1);
1757 * Calculate 3D coordinates on sphere for corresponding frame position in sinusoidal format.
1759 * @param s filter private context
1760 * @param i horizontal position on frame [0, width)
1761 * @param j vertical position on frame [0, height)
1762 * @param width frame width
1763 * @param height frame height
1764 * @param vec coordinates on sphere
1766 static void sinusoidal_to_xyz(const V360Context *s,
1767 int i, int j, int width, int height,
1770 const float theta = ((2.f * j) / height - 1.f) * M_PI_2;
1771 const float phi = ((2.f * i) / width - 1.f) * M_PI / cosf(theta);
1773 const float sin_phi = sinf(phi);
1774 const float cos_phi = cosf(phi);
1775 const float sin_theta = sinf(theta);
1776 const float cos_theta = cosf(theta);
1778 vec[0] = cos_theta * sin_phi;
1779 vec[1] = -sin_theta;
1780 vec[2] = -cos_theta * cos_phi;
1782 normalize_vector(vec);
1786 * Calculate frame position in sinusoidal format for corresponding 3D coordinates on sphere.
1788 * @param s filter private context
1789 * @param vec coordinates on sphere
1790 * @param width frame width
1791 * @param height frame height
1792 * @param us horizontal coordinates for interpolation window
1793 * @param vs vertical coordinates for interpolation window
1794 * @param du horizontal relative coordinate
1795 * @param dv vertical relative coordinate
1797 static void xyz_to_sinusoidal(const V360Context *s,
1798 const float *vec, int width, int height,
1799 uint16_t us[4][4], uint16_t vs[4][4], float *du, float *dv)
1801 const float theta = asinf(-vec[1]) * s->input_mirror_modifier[1];
1802 const float phi = atan2f(vec[0], -vec[2]) * s->input_mirror_modifier[0] * cosf(theta);
1806 uf = (phi / M_PI + 1.f) * width / 2.f;
1807 vf = (theta / M_PI_2 + 1.f) * height / 2.f;
1814 for (int i = -1; i < 3; i++) {
1815 for (int j = -1; j < 3; j++) {
1816 us[i + 1][j + 1] = av_clip(ui + j, 0, width - 1);
1817 vs[i + 1][j + 1] = av_clip(vi + i, 0, height - 1);
1823 * Prepare data for processing equi-angular cubemap input format.
1825 * @param ctx filter context
1827 * @return error code
1829 static int prepare_eac_in(AVFilterContext *ctx)
1831 V360Context *s = ctx->priv;
1833 if (s->ih_flip && s->iv_flip) {
1834 s->in_cubemap_face_order[RIGHT] = BOTTOM_LEFT;
1835 s->in_cubemap_face_order[LEFT] = BOTTOM_RIGHT;
1836 s->in_cubemap_face_order[UP] = TOP_LEFT;
1837 s->in_cubemap_face_order[DOWN] = TOP_RIGHT;
1838 s->in_cubemap_face_order[FRONT] = BOTTOM_MIDDLE;
1839 s->in_cubemap_face_order[BACK] = TOP_MIDDLE;
1840 } else if (s->ih_flip) {
1841 s->in_cubemap_face_order[RIGHT] = TOP_LEFT;
1842 s->in_cubemap_face_order[LEFT] = TOP_RIGHT;
1843 s->in_cubemap_face_order[UP] = BOTTOM_LEFT;
1844 s->in_cubemap_face_order[DOWN] = BOTTOM_RIGHT;
1845 s->in_cubemap_face_order[FRONT] = TOP_MIDDLE;
1846 s->in_cubemap_face_order[BACK] = BOTTOM_MIDDLE;
1847 } else if (s->iv_flip) {
1848 s->in_cubemap_face_order[RIGHT] = BOTTOM_RIGHT;
1849 s->in_cubemap_face_order[LEFT] = BOTTOM_LEFT;
1850 s->in_cubemap_face_order[UP] = TOP_RIGHT;
1851 s->in_cubemap_face_order[DOWN] = TOP_LEFT;
1852 s->in_cubemap_face_order[FRONT] = BOTTOM_MIDDLE;
1853 s->in_cubemap_face_order[BACK] = TOP_MIDDLE;
1855 s->in_cubemap_face_order[RIGHT] = TOP_RIGHT;
1856 s->in_cubemap_face_order[LEFT] = TOP_LEFT;
1857 s->in_cubemap_face_order[UP] = BOTTOM_RIGHT;
1858 s->in_cubemap_face_order[DOWN] = BOTTOM_LEFT;
1859 s->in_cubemap_face_order[FRONT] = TOP_MIDDLE;
1860 s->in_cubemap_face_order[BACK] = BOTTOM_MIDDLE;
1864 s->in_cubemap_face_rotation[TOP_LEFT] = ROT_270;
1865 s->in_cubemap_face_rotation[TOP_MIDDLE] = ROT_90;
1866 s->in_cubemap_face_rotation[TOP_RIGHT] = ROT_270;
1867 s->in_cubemap_face_rotation[BOTTOM_LEFT] = ROT_0;
1868 s->in_cubemap_face_rotation[BOTTOM_MIDDLE] = ROT_0;
1869 s->in_cubemap_face_rotation[BOTTOM_RIGHT] = ROT_0;
1871 s->in_cubemap_face_rotation[TOP_LEFT] = ROT_0;
1872 s->in_cubemap_face_rotation[TOP_MIDDLE] = ROT_0;
1873 s->in_cubemap_face_rotation[TOP_RIGHT] = ROT_0;
1874 s->in_cubemap_face_rotation[BOTTOM_LEFT] = ROT_270;
1875 s->in_cubemap_face_rotation[BOTTOM_MIDDLE] = ROT_90;
1876 s->in_cubemap_face_rotation[BOTTOM_RIGHT] = ROT_270;
1883 * Prepare data for processing equi-angular cubemap output format.
1885 * @param ctx filter context
1887 * @return error code
1889 static int prepare_eac_out(AVFilterContext *ctx)
1891 V360Context *s = ctx->priv;
1893 s->out_cubemap_direction_order[TOP_LEFT] = LEFT;
1894 s->out_cubemap_direction_order[TOP_MIDDLE] = FRONT;
1895 s->out_cubemap_direction_order[TOP_RIGHT] = RIGHT;
1896 s->out_cubemap_direction_order[BOTTOM_LEFT] = DOWN;
1897 s->out_cubemap_direction_order[BOTTOM_MIDDLE] = BACK;
1898 s->out_cubemap_direction_order[BOTTOM_RIGHT] = UP;
1900 s->out_cubemap_face_rotation[TOP_LEFT] = ROT_0;
1901 s->out_cubemap_face_rotation[TOP_MIDDLE] = ROT_0;
1902 s->out_cubemap_face_rotation[TOP_RIGHT] = ROT_0;
1903 s->out_cubemap_face_rotation[BOTTOM_LEFT] = ROT_270;
1904 s->out_cubemap_face_rotation[BOTTOM_MIDDLE] = ROT_90;
1905 s->out_cubemap_face_rotation[BOTTOM_RIGHT] = ROT_270;
1911 * Calculate 3D coordinates on sphere for corresponding frame position in equi-angular cubemap format.
1913 * @param s filter private context
1914 * @param i horizontal position on frame [0, width)
1915 * @param j vertical position on frame [0, height)
1916 * @param width frame width
1917 * @param height frame height
1918 * @param vec coordinates on sphere
1920 static void eac_to_xyz(const V360Context *s,
1921 int i, int j, int width, int height,
1924 const float pixel_pad = 2;
1925 const float u_pad = pixel_pad / width;
1926 const float v_pad = pixel_pad / height;
1928 int u_face, v_face, face;
1930 float l_x, l_y, l_z;
1932 float uf = (i + 0.5f) / width;
1933 float vf = (j + 0.5f) / height;
1935 // EAC has 2-pixel padding on faces except between faces on the same row
1936 // Padding pixels seems not to be stretched with tangent as regular pixels
1937 // Formulas below approximate original padding as close as I could get experimentally
1939 // Horizontal padding
1940 uf = 3.f * (uf - u_pad) / (1.f - 2.f * u_pad);
1944 } else if (uf >= 3.f) {
1948 u_face = floorf(uf);
1949 uf = fmodf(uf, 1.f) - 0.5f;
1953 v_face = floorf(vf * 2.f);
1954 vf = (vf - v_pad - 0.5f * v_face) / (0.5f - 2.f * v_pad) - 0.5f;
1956 if (uf >= -0.5f && uf < 0.5f) {
1957 uf = tanf(M_PI_2 * uf);
1961 if (vf >= -0.5f && vf < 0.5f) {
1962 vf = tanf(M_PI_2 * vf);
1967 face = u_face + 3 * v_face;
2008 normalize_vector(vec);
2012 * Calculate frame position in equi-angular cubemap format for corresponding 3D coordinates on sphere.
2014 * @param s filter private context
2015 * @param vec coordinates on sphere
2016 * @param width frame width
2017 * @param height frame height
2018 * @param us horizontal coordinates for interpolation window
2019 * @param vs vertical coordinates for interpolation window
2020 * @param du horizontal relative coordinate
2021 * @param dv vertical relative coordinate
2023 static void xyz_to_eac(const V360Context *s,
2024 const float *vec, int width, int height,
2025 uint16_t us[4][4], uint16_t vs[4][4], float *du, float *dv)
2027 const float pixel_pad = 2;
2028 const float u_pad = pixel_pad / width;
2029 const float v_pad = pixel_pad / height;
2033 int direction, face;
2036 xyz_to_cube(s, vec, &uf, &vf, &direction);
2038 face = s->in_cubemap_face_order[direction];
2042 uf = M_2_PI * atanf(uf) + 0.5f;
2043 vf = M_2_PI * atanf(vf) + 0.5f;
2045 // These formulas are inversed from eac_to_xyz ones
2046 uf = (uf + u_face) * (1.f - 2.f * u_pad) / 3.f + u_pad;
2047 vf = vf * (0.5f - 2.f * v_pad) + v_pad + 0.5f * v_face;
2061 for (int i = -1; i < 3; i++) {
2062 for (int j = -1; j < 3; j++) {
2063 us[i + 1][j + 1] = av_clip(ui + j, 0, width - 1);
2064 vs[i + 1][j + 1] = av_clip(vi + i, 0, height - 1);
2070 * Prepare data for processing flat output format.
2072 * @param ctx filter context
2074 * @return error code
2076 static int prepare_flat_out(AVFilterContext *ctx)
2078 V360Context *s = ctx->priv;
2080 s->flat_range[0] = tanf(0.5f * s->h_fov * M_PI / 180.f);
2081 s->flat_range[1] = tanf(0.5f * s->v_fov * M_PI / 180.f);
2087 * Calculate 3D coordinates on sphere for corresponding frame position in flat format.
2089 * @param s filter private context
2090 * @param i horizontal position on frame [0, width)
2091 * @param j vertical position on frame [0, height)
2092 * @param width frame width
2093 * @param height frame height
2094 * @param vec coordinates on sphere
2096 static void flat_to_xyz(const V360Context *s,
2097 int i, int j, int width, int height,
2100 const float l_x = s->flat_range[0] * (2.f * i / width - 1.f);
2101 const float l_y = -s->flat_range[1] * (2.f * j / height - 1.f);
2107 normalize_vector(vec);
2111 * Prepare data for processing fisheye output format.
2113 * @param ctx filter context
2115 * @return error code
2117 static int prepare_fisheye_out(AVFilterContext *ctx)
2119 V360Context *s = ctx->priv;
2121 s->flat_range[0] = FFMIN(s->h_fov, 359.f) / 180.f;
2122 s->flat_range[1] = FFMIN(s->v_fov, 359.f) / 180.f;
2128 * Calculate 3D coordinates on sphere for corresponding frame position in fisheye format.
2130 * @param s filter private context
2131 * @param i horizontal position on frame [0, width)
2132 * @param j vertical position on frame [0, height)
2133 * @param width frame width
2134 * @param height frame height
2135 * @param vec coordinates on sphere
2137 static void fisheye_to_xyz(const V360Context *s,
2138 int i, int j, int width, int height,
2141 const float uf = s->flat_range[0] * ((2.f * i) / width - 1.f);
2142 const float vf = s->flat_range[1] * ((2.f * j) / height - 1.f);
2144 const float phi = -atan2f(vf, uf);
2145 const float theta = -M_PI_2 * (1.f - hypotf(uf, vf));
2147 vec[0] = cosf(theta) * cosf(phi);
2148 vec[1] = cosf(theta) * sinf(phi);
2149 vec[2] = sinf(theta);
2151 normalize_vector(vec);
2155 * Calculate 3D coordinates on sphere for corresponding frame position in pannini format.
2157 * @param s filter private context
2158 * @param i horizontal position on frame [0, width)
2159 * @param j vertical position on frame [0, height)
2160 * @param width frame width
2161 * @param height frame height
2162 * @param vec coordinates on sphere
2164 static void pannini_to_xyz(const V360Context *s,
2165 int i, int j, int width, int height,
2168 const float uf = ((2.f * i) / width - 1.f);
2169 const float vf = ((2.f * j) / height - 1.f);
2171 const float d = s->h_fov;
2172 float k = uf * uf / ((d + 1.f) * (d + 1.f));
2173 float dscr = k * k * d * d - (k + 1) * (k * d * d - 1.f);
2174 float clon = (-k * d + sqrtf(dscr)) / (k + 1.f);
2175 float S = (d + 1.f) / (d + clon);
2176 float lon = -(M_PI + atan2f(uf, S * clon));
2177 float lat = -atan2f(vf, S);
2179 vec[0] = sinf(lon) * cosf(lat);
2181 vec[2] = cosf(lon) * cosf(lat);
2183 normalize_vector(vec);
2187 * Calculate 3D coordinates on sphere for corresponding frame position in dual fisheye format.
2189 * @param s filter private context
2190 * @param i horizontal position on frame [0, width)
2191 * @param j vertical position on frame [0, height)
2192 * @param width frame width
2193 * @param height frame height
2194 * @param vec coordinates on sphere
2196 static void dfisheye_to_xyz(const V360Context *s,
2197 int i, int j, int width, int height,
2200 const float scale = 1.f + s->out_pad;
2202 const float ew = width / 2.f;
2203 const float eh = height;
2205 const int ei = i >= ew ? i - ew : i;
2206 const float m = i >= ew ? -1.f : 1.f;
2208 const float uf = ((2.f * ei) / ew - 1.f) * scale;
2209 const float vf = ((2.f * j) / eh - 1.f) * scale;
2211 const float h = hypotf(uf, vf);
2212 const float lh = h > 0.f ? h : 1.f;
2213 const float theta = m * M_PI_2 * (1.f - h);
2215 const float sin_theta = sinf(theta);
2216 const float cos_theta = cosf(theta);
2218 vec[0] = cos_theta * m * -uf / lh;
2219 vec[1] = cos_theta * -vf / lh;
2222 normalize_vector(vec);
2226 * Calculate frame position in dual fisheye format for corresponding 3D coordinates on sphere.
2228 * @param s filter private context
2229 * @param vec coordinates on sphere
2230 * @param width frame width
2231 * @param height frame height
2232 * @param us horizontal coordinates for interpolation window
2233 * @param vs vertical coordinates for interpolation window
2234 * @param du horizontal relative coordinate
2235 * @param dv vertical relative coordinate
2237 static void xyz_to_dfisheye(const V360Context *s,
2238 const float *vec, int width, int height,
2239 uint16_t us[4][4], uint16_t vs[4][4], float *du, float *dv)
2241 const float scale = 1.f - s->in_pad;
2243 const float ew = width / 2.f;
2244 const float eh = height;
2246 const float h = hypotf(vec[0], vec[1]);
2247 const float lh = h > 0.f ? h : 1.f;
2248 const float theta = acosf(fabsf(vec[2])) / M_PI;
2250 float uf = (theta * (-vec[0] / lh) * s->input_mirror_modifier[0] * scale + 0.5f) * ew;
2251 float vf = (theta * (-vec[1] / lh) * s->input_mirror_modifier[1] * scale + 0.5f) * eh;
2256 if (vec[2] >= 0.f) {
2259 u_shift = ceilf(ew);
2269 for (int i = -1; i < 3; i++) {
2270 for (int j = -1; j < 3; j++) {
2271 us[i + 1][j + 1] = av_clip(u_shift + ui + j, 0, width - 1);
2272 vs[i + 1][j + 1] = av_clip( vi + i, 0, height - 1);
2278 * Calculate 3D coordinates on sphere for corresponding frame position in barrel facebook's format.
2280 * @param s filter private context
2281 * @param i horizontal position on frame [0, width)
2282 * @param j vertical position on frame [0, height)
2283 * @param width frame width
2284 * @param height frame height
2285 * @param vec coordinates on sphere
2287 static void barrel_to_xyz(const V360Context *s,
2288 int i, int j, int width, int height,
2291 const float scale = 0.99f;
2292 float l_x, l_y, l_z;
2294 if (i < 4 * width / 5) {
2295 const float theta_range = M_PI_4;
2297 const int ew = 4 * width / 5;
2298 const int eh = height;
2300 const float phi = ((2.f * i) / ew - 1.f) * M_PI / scale;
2301 const float theta = ((2.f * j) / eh - 1.f) * theta_range / scale;
2303 const float sin_phi = sinf(phi);
2304 const float cos_phi = cosf(phi);
2305 const float sin_theta = sinf(theta);
2306 const float cos_theta = cosf(theta);
2308 l_x = cos_theta * sin_phi;
2310 l_z = -cos_theta * cos_phi;
2312 const int ew = width / 5;
2313 const int eh = height / 2;
2318 uf = 2.f * (i - 4 * ew) / ew - 1.f;
2319 vf = 2.f * (j ) / eh - 1.f;
2328 uf = 2.f * (i - 4 * ew) / ew - 1.f;
2329 vf = 2.f * (j - eh) / eh - 1.f;
2344 normalize_vector(vec);
2348 * Calculate frame position in barrel facebook's format for corresponding 3D coordinates on sphere.
2350 * @param s filter private context
2351 * @param vec coordinates on sphere
2352 * @param width frame width
2353 * @param height frame height
2354 * @param us horizontal coordinates for interpolation window
2355 * @param vs vertical coordinates for interpolation window
2356 * @param du horizontal relative coordinate
2357 * @param dv vertical relative coordinate
2359 static void xyz_to_barrel(const V360Context *s,
2360 const float *vec, int width, int height,
2361 uint16_t us[4][4], uint16_t vs[4][4], float *du, float *dv)
2363 const float scale = 0.99f;
2365 const float phi = atan2f(vec[0], -vec[2]) * s->input_mirror_modifier[0];
2366 const float theta = asinf(-vec[1]) * s->input_mirror_modifier[1];
2367 const float theta_range = M_PI_4;
2370 int u_shift, v_shift;
2374 if (theta > -theta_range && theta < theta_range) {
2378 u_shift = s->ih_flip ? width / 5 : 0;
2381 uf = (phi / M_PI * scale + 1.f) * ew / 2.f;
2382 vf = (theta / theta_range * scale + 1.f) * eh / 2.f;
2387 u_shift = s->ih_flip ? 0 : 4 * ew;
2389 if (theta < 0.f) { // UP
2390 uf = vec[0] / vec[1];
2391 vf = -vec[2] / vec[1];
2394 uf = -vec[0] / vec[1];
2395 vf = -vec[2] / vec[1];
2399 uf *= s->input_mirror_modifier[0] * s->input_mirror_modifier[1];
2400 vf *= s->input_mirror_modifier[1];
2402 uf = 0.5f * ew * (uf * scale + 1.f);
2403 vf = 0.5f * eh * (vf * scale + 1.f);
2412 for (int i = -1; i < 3; i++) {
2413 for (int j = -1; j < 3; j++) {
2414 us[i + 1][j + 1] = u_shift + av_clip(ui + j, 0, ew - 1);
2415 vs[i + 1][j + 1] = v_shift + av_clip(vi + i, 0, eh - 1);
2420 static void multiply_matrix(float c[3][3], const float a[3][3], const float b[3][3])
2422 for (int i = 0; i < 3; i++) {
2423 for (int j = 0; j < 3; j++) {
2426 for (int k = 0; k < 3; k++)
2427 sum += a[i][k] * b[k][j];
2435 * Calculate rotation matrix for yaw/pitch/roll angles.
2437 static inline void calculate_rotation_matrix(float yaw, float pitch, float roll,
2438 float rot_mat[3][3],
2439 const int rotation_order[3])
2441 const float yaw_rad = yaw * M_PI / 180.f;
2442 const float pitch_rad = pitch * M_PI / 180.f;
2443 const float roll_rad = roll * M_PI / 180.f;
2445 const float sin_yaw = sinf(-yaw_rad);
2446 const float cos_yaw = cosf(-yaw_rad);
2447 const float sin_pitch = sinf(pitch_rad);
2448 const float cos_pitch = cosf(pitch_rad);
2449 const float sin_roll = sinf(roll_rad);
2450 const float cos_roll = cosf(roll_rad);
2455 m[0][0][0] = cos_yaw; m[0][0][1] = 0; m[0][0][2] = sin_yaw;
2456 m[0][1][0] = 0; m[0][1][1] = 1; m[0][1][2] = 0;
2457 m[0][2][0] = -sin_yaw; m[0][2][1] = 0; m[0][2][2] = cos_yaw;
2459 m[1][0][0] = 1; m[1][0][1] = 0; m[1][0][2] = 0;
2460 m[1][1][0] = 0; m[1][1][1] = cos_pitch; m[1][1][2] = -sin_pitch;
2461 m[1][2][0] = 0; m[1][2][1] = sin_pitch; m[1][2][2] = cos_pitch;
2463 m[2][0][0] = cos_roll; m[2][0][1] = -sin_roll; m[2][0][2] = 0;
2464 m[2][1][0] = sin_roll; m[2][1][1] = cos_roll; m[2][1][2] = 0;
2465 m[2][2][0] = 0; m[2][2][1] = 0; m[2][2][2] = 1;
2467 multiply_matrix(temp, m[rotation_order[0]], m[rotation_order[1]]);
2468 multiply_matrix(rot_mat, temp, m[rotation_order[2]]);
2472 * Rotate vector with given rotation matrix.
2474 * @param rot_mat rotation matrix
2477 static inline void rotate(const float rot_mat[3][3],
2480 const float x_tmp = vec[0] * rot_mat[0][0] + vec[1] * rot_mat[0][1] + vec[2] * rot_mat[0][2];
2481 const float y_tmp = vec[0] * rot_mat[1][0] + vec[1] * rot_mat[1][1] + vec[2] * rot_mat[1][2];
2482 const float z_tmp = vec[0] * rot_mat[2][0] + vec[1] * rot_mat[2][1] + vec[2] * rot_mat[2][2];
2489 static inline void set_mirror_modifier(int h_flip, int v_flip, int d_flip,
2492 modifier[0] = h_flip ? -1.f : 1.f;
2493 modifier[1] = v_flip ? -1.f : 1.f;
2494 modifier[2] = d_flip ? -1.f : 1.f;
2497 static inline void mirror(const float *modifier, float *vec)
2499 vec[0] *= modifier[0];
2500 vec[1] *= modifier[1];
2501 vec[2] *= modifier[2];
2504 static int allocate_plane(V360Context *s, int sizeof_uv, int sizeof_ker, int p)
2506 s->u[p] = av_calloc(s->uv_linesize[p] * s->pr_height[p], sizeof_uv);
2507 s->v[p] = av_calloc(s->uv_linesize[p] * s->pr_height[p], sizeof_uv);
2508 if (!s->u[p] || !s->v[p])
2509 return AVERROR(ENOMEM);
2511 s->ker[p] = av_calloc(s->uv_linesize[p] * s->pr_height[p], sizeof_ker);
2513 return AVERROR(ENOMEM);
2519 static void fov_from_dfov(V360Context *s, float w, float h)
2521 const float da = tanf(0.5 * FFMIN(s->d_fov, 359.f) * M_PI / 180.f);
2522 const float d = hypotf(w, h);
2524 s->h_fov = atan2f(da * w, d) * 360.f / M_PI;
2525 s->v_fov = atan2f(da * h, d) * 360.f / M_PI;
2533 static void set_dimensions(int *outw, int *outh, int w, int h, const AVPixFmtDescriptor *desc)
2535 outw[1] = outw[2] = FF_CEIL_RSHIFT(w, desc->log2_chroma_w);
2536 outw[0] = outw[3] = w;
2537 outh[1] = outh[2] = FF_CEIL_RSHIFT(h, desc->log2_chroma_h);
2538 outh[0] = outh[3] = h;
2541 // Calculate remap data
2542 static av_always_inline int v360_slice(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
2544 V360Context *s = ctx->priv;
2546 for (int p = 0; p < s->nb_allocated; p++) {
2547 const int width = s->pr_width[p];
2548 const int uv_linesize = s->uv_linesize[p];
2549 const int height = s->pr_height[p];
2550 const int in_width = s->inplanewidth[p];
2551 const int in_height = s->inplaneheight[p];
2552 const int slice_start = (height * jobnr ) / nb_jobs;
2553 const int slice_end = (height * (jobnr + 1)) / nb_jobs;
2558 for (int j = slice_start; j < slice_end; j++) {
2559 for (int i = 0; i < width; i++) {
2560 uint16_t *u = s->u[p] + (j * uv_linesize + i) * s->elements;
2561 uint16_t *v = s->v[p] + (j * uv_linesize + i) * s->elements;
2562 int16_t *ker = s->ker[p] + (j * uv_linesize + i) * s->elements;
2564 if (s->out_transpose)
2565 s->out_transform(s, j, i, height, width, vec);
2567 s->out_transform(s, i, j, width, height, vec);
2568 av_assert1(!isnan(vec[0]) && !isnan(vec[1]) && !isnan(vec[2]));
2569 rotate(s->rot_mat, vec);
2570 av_assert1(!isnan(vec[0]) && !isnan(vec[1]) && !isnan(vec[2]));
2571 normalize_vector(vec);
2572 mirror(s->output_mirror_modifier, vec);
2573 if (s->in_transpose)
2574 s->in_transform(s, vec, in_height, in_width, rmap.v, rmap.u, &du, &dv);
2576 s->in_transform(s, vec, in_width, in_height, rmap.u, rmap.v, &du, &dv);
2577 av_assert1(!isnan(du) && !isnan(dv));
2578 s->calculate_kernel(du, dv, &rmap, u, v, ker);
2586 static int config_output(AVFilterLink *outlink)
2588 AVFilterContext *ctx = outlink->src;
2589 AVFilterLink *inlink = ctx->inputs[0];
2590 V360Context *s = ctx->priv;
2591 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(inlink->format);
2592 const int depth = desc->comp[0].depth;
2597 int in_offset_h, in_offset_w;
2598 int out_offset_h, out_offset_w;
2600 int (*prepare_out)(AVFilterContext *ctx);
2602 s->input_mirror_modifier[0] = s->ih_flip ? -1.f : 1.f;
2603 s->input_mirror_modifier[1] = s->iv_flip ? -1.f : 1.f;
2605 switch (s->interp) {
2607 s->calculate_kernel = nearest_kernel;
2608 s->remap_slice = depth <= 8 ? remap1_8bit_slice : remap1_16bit_slice;
2610 sizeof_uv = sizeof(uint16_t) * s->elements;
2614 s->calculate_kernel = bilinear_kernel;
2615 s->remap_slice = depth <= 8 ? remap2_8bit_slice : remap2_16bit_slice;
2616 s->elements = 2 * 2;
2617 sizeof_uv = sizeof(uint16_t) * s->elements;
2618 sizeof_ker = sizeof(uint16_t) * s->elements;
2621 s->calculate_kernel = bicubic_kernel;
2622 s->remap_slice = depth <= 8 ? remap4_8bit_slice : remap4_16bit_slice;
2623 s->elements = 4 * 4;
2624 sizeof_uv = sizeof(uint16_t) * s->elements;
2625 sizeof_ker = sizeof(uint16_t) * s->elements;
2628 s->calculate_kernel = lanczos_kernel;
2629 s->remap_slice = depth <= 8 ? remap4_8bit_slice : remap4_16bit_slice;
2630 s->elements = 4 * 4;
2631 sizeof_uv = sizeof(uint16_t) * s->elements;
2632 sizeof_ker = sizeof(uint16_t) * s->elements;
2638 ff_v360_init(s, depth);
2640 for (int order = 0; order < NB_RORDERS; order++) {
2641 const char c = s->rorder[order];
2645 av_log(ctx, AV_LOG_ERROR,
2646 "Incomplete rorder option. Direction for all 3 rotation orders should be specified.\n");
2647 return AVERROR(EINVAL);
2650 rorder = get_rorder(c);
2652 av_log(ctx, AV_LOG_ERROR,
2653 "Incorrect rotation order symbol '%c' in rorder option.\n", c);
2654 return AVERROR(EINVAL);
2657 s->rotation_order[order] = rorder;
2660 switch (s->in_stereo) {
2664 in_offset_w = in_offset_h = 0;
2682 set_dimensions(s->inplanewidth, s->inplaneheight, w, h, desc);
2683 set_dimensions(s->in_offset_w, s->in_offset_h, in_offset_w, in_offset_h, desc);
2685 s->in_width = s->inplanewidth[0];
2686 s->in_height = s->inplaneheight[0];
2688 if (s->in_transpose)
2689 FFSWAP(int, s->in_width, s->in_height);
2692 case EQUIRECTANGULAR:
2693 s->in_transform = xyz_to_equirect;
2699 s->in_transform = xyz_to_cube3x2;
2700 err = prepare_cube_in(ctx);
2705 s->in_transform = xyz_to_cube1x6;
2706 err = prepare_cube_in(ctx);
2711 s->in_transform = xyz_to_cube6x1;
2712 err = prepare_cube_in(ctx);
2717 s->in_transform = xyz_to_eac;
2718 err = prepare_eac_in(ctx);
2725 av_log(ctx, AV_LOG_ERROR, "Supplied format is not accepted as input.\n");
2726 return AVERROR(EINVAL);
2728 s->in_transform = xyz_to_dfisheye;
2734 s->in_transform = xyz_to_barrel;
2740 s->in_transform = xyz_to_stereographic;
2746 s->in_transform = xyz_to_mercator;
2752 s->in_transform = xyz_to_ball;
2758 s->in_transform = xyz_to_hammer;
2764 s->in_transform = xyz_to_sinusoidal;
2770 av_log(ctx, AV_LOG_ERROR, "Specified input format is not handled.\n");
2779 case EQUIRECTANGULAR:
2780 s->out_transform = equirect_to_xyz;
2786 s->out_transform = cube3x2_to_xyz;
2787 prepare_out = prepare_cube_out;
2788 w = roundf(wf / 4.f * 3.f);
2792 s->out_transform = cube1x6_to_xyz;
2793 prepare_out = prepare_cube_out;
2794 w = roundf(wf / 4.f);
2795 h = roundf(hf * 3.f);
2798 s->out_transform = cube6x1_to_xyz;
2799 prepare_out = prepare_cube_out;
2800 w = roundf(wf / 2.f * 3.f);
2801 h = roundf(hf / 2.f);
2804 s->out_transform = eac_to_xyz;
2805 prepare_out = prepare_eac_out;
2807 h = roundf(hf / 8.f * 9.f);
2810 s->out_transform = flat_to_xyz;
2811 prepare_out = prepare_flat_out;
2816 s->out_transform = dfisheye_to_xyz;
2822 s->out_transform = barrel_to_xyz;
2824 w = roundf(wf / 4.f * 5.f);
2828 s->out_transform = stereographic_to_xyz;
2829 prepare_out = prepare_stereographic_out;
2831 h = roundf(hf * 2.f);
2834 s->out_transform = mercator_to_xyz;
2837 h = roundf(hf * 2.f);
2840 s->out_transform = ball_to_xyz;
2843 h = roundf(hf * 2.f);
2846 s->out_transform = hammer_to_xyz;
2852 s->out_transform = sinusoidal_to_xyz;
2858 s->out_transform = fisheye_to_xyz;
2859 prepare_out = prepare_fisheye_out;
2860 w = roundf(wf * 0.5f);
2864 s->out_transform = pannini_to_xyz;
2865 prepare_out = prepare_fisheye_out;
2870 av_log(ctx, AV_LOG_ERROR, "Specified output format is not handled.\n");
2874 // Override resolution with user values if specified
2875 if (s->width > 0 && s->height > 0) {
2878 } else if (s->width > 0 || s->height > 0) {
2879 av_log(ctx, AV_LOG_ERROR, "Both width and height values should be specified.\n");
2880 return AVERROR(EINVAL);
2882 if (s->out_transpose)
2885 if (s->in_transpose)
2890 fov_from_dfov(s, w, h);
2893 err = prepare_out(ctx);
2898 set_dimensions(s->pr_width, s->pr_height, w, h, desc);
2900 s->out_width = s->pr_width[0];
2901 s->out_height = s->pr_height[0];
2903 if (s->out_transpose)
2904 FFSWAP(int, s->out_width, s->out_height);
2906 switch (s->out_stereo) {
2908 out_offset_w = out_offset_h = 0;
2924 set_dimensions(s->out_offset_w, s->out_offset_h, out_offset_w, out_offset_h, desc);
2925 set_dimensions(s->planewidth, s->planeheight, w, h, desc);
2927 for (int i = 0; i < 4; i++)
2928 s->uv_linesize[i] = FFALIGN(s->pr_width[i], 8);
2933 s->nb_planes = av_pix_fmt_count_planes(inlink->format);
2935 if (desc->log2_chroma_h == desc->log2_chroma_w && desc->log2_chroma_h == 0) {
2936 s->nb_allocated = 1;
2937 s->map[0] = s->map[1] = s->map[2] = s->map[3] = 0;
2939 s->nb_allocated = 2;
2941 s->map[1] = s->map[2] = 1;
2945 for (int i = 0; i < s->nb_allocated; i++)
2946 allocate_plane(s, sizeof_uv, sizeof_ker, i);
2948 calculate_rotation_matrix(s->yaw, s->pitch, s->roll, s->rot_mat, s->rotation_order);
2949 set_mirror_modifier(s->h_flip, s->v_flip, s->d_flip, s->output_mirror_modifier);
2951 ctx->internal->execute(ctx, v360_slice, NULL, NULL, FFMIN(outlink->h, ff_filter_get_nb_threads(ctx)));
2956 static int filter_frame(AVFilterLink *inlink, AVFrame *in)
2958 AVFilterContext *ctx = inlink->dst;
2959 AVFilterLink *outlink = ctx->outputs[0];
2960 V360Context *s = ctx->priv;
2964 out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
2967 return AVERROR(ENOMEM);
2969 av_frame_copy_props(out, in);
2974 ctx->internal->execute(ctx, s->remap_slice, &td, NULL, FFMIN(outlink->h, ff_filter_get_nb_threads(ctx)));
2977 return ff_filter_frame(outlink, out);
2980 static av_cold void uninit(AVFilterContext *ctx)
2982 V360Context *s = ctx->priv;
2984 for (int p = 0; p < s->nb_allocated; p++) {
2987 av_freep(&s->ker[p]);
2991 static const AVFilterPad inputs[] = {
2994 .type = AVMEDIA_TYPE_VIDEO,
2995 .filter_frame = filter_frame,
3000 static const AVFilterPad outputs[] = {
3003 .type = AVMEDIA_TYPE_VIDEO,
3004 .config_props = config_output,
3009 AVFilter ff_vf_v360 = {
3011 .description = NULL_IF_CONFIG_SMALL("Convert 360 projection of video."),
3012 .priv_size = sizeof(V360Context),
3014 .query_formats = query_formats,
3017 .priv_class = &v360_class,
3018 .flags = AVFILTER_FLAG_SLICE_THREADS,