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 { "interp", "set interpolation method", OFFSET(interp), AV_OPT_TYPE_INT, {.i64=BILINEAR}, 0, NB_INTERP_METHODS-1, FLAGS, "interp" },
91 { "near", "nearest neighbour", 0, AV_OPT_TYPE_CONST, {.i64=NEAREST}, 0, 0, FLAGS, "interp" },
92 { "nearest", "nearest neighbour", 0, AV_OPT_TYPE_CONST, {.i64=NEAREST}, 0, 0, FLAGS, "interp" },
93 { "line", "bilinear interpolation", 0, AV_OPT_TYPE_CONST, {.i64=BILINEAR}, 0, 0, FLAGS, "interp" },
94 { "linear", "bilinear interpolation", 0, AV_OPT_TYPE_CONST, {.i64=BILINEAR}, 0, 0, FLAGS, "interp" },
95 { "cube", "bicubic interpolation", 0, AV_OPT_TYPE_CONST, {.i64=BICUBIC}, 0, 0, FLAGS, "interp" },
96 { "cubic", "bicubic interpolation", 0, AV_OPT_TYPE_CONST, {.i64=BICUBIC}, 0, 0, FLAGS, "interp" },
97 { "lanc", "lanczos interpolation", 0, AV_OPT_TYPE_CONST, {.i64=LANCZOS}, 0, 0, FLAGS, "interp" },
98 { "lanczos", "lanczos interpolation", 0, AV_OPT_TYPE_CONST, {.i64=LANCZOS}, 0, 0, FLAGS, "interp" },
99 { "w", "output width", OFFSET(width), AV_OPT_TYPE_INT, {.i64=0}, 0, INT16_MAX, FLAGS, "w"},
100 { "h", "output height", OFFSET(height), AV_OPT_TYPE_INT, {.i64=0}, 0, INT16_MAX, FLAGS, "h"},
101 { "in_stereo", "input stereo format", OFFSET(in_stereo), AV_OPT_TYPE_INT, {.i64=STEREO_2D}, 0, NB_STEREO_FMTS-1, FLAGS, "stereo" },
102 {"out_stereo", "output stereo format", OFFSET(out_stereo), AV_OPT_TYPE_INT, {.i64=STEREO_2D}, 0, NB_STEREO_FMTS-1, FLAGS, "stereo" },
103 { "2d", "2d mono", 0, AV_OPT_TYPE_CONST, {.i64=STEREO_2D}, 0, 0, FLAGS, "stereo" },
104 { "sbs", "side by side", 0, AV_OPT_TYPE_CONST, {.i64=STEREO_SBS}, 0, 0, FLAGS, "stereo" },
105 { "tb", "top bottom", 0, AV_OPT_TYPE_CONST, {.i64=STEREO_TB}, 0, 0, FLAGS, "stereo" },
106 { "in_forder", "input cubemap face order", OFFSET(in_forder), AV_OPT_TYPE_STRING, {.str="rludfb"}, 0, NB_DIRECTIONS-1, FLAGS, "in_forder"},
107 {"out_forder", "output cubemap face order", OFFSET(out_forder), AV_OPT_TYPE_STRING, {.str="rludfb"}, 0, NB_DIRECTIONS-1, FLAGS, "out_forder"},
108 { "in_frot", "input cubemap face rotation", OFFSET(in_frot), AV_OPT_TYPE_STRING, {.str="000000"}, 0, NB_DIRECTIONS-1, FLAGS, "in_frot"},
109 { "out_frot", "output cubemap face rotation",OFFSET(out_frot), AV_OPT_TYPE_STRING, {.str="000000"}, 0, NB_DIRECTIONS-1, FLAGS, "out_frot"},
110 { "in_pad", "percent input cubemap pads", OFFSET(in_pad), AV_OPT_TYPE_FLOAT, {.dbl=0.f}, 0.f, 1.f, FLAGS, "in_pad"},
111 { "out_pad", "percent output cubemap pads", OFFSET(out_pad), AV_OPT_TYPE_FLOAT, {.dbl=0.f}, 0.f, 1.f, FLAGS, "out_pad"},
112 { "fin_pad", "fixed input cubemap pads", OFFSET(fin_pad), AV_OPT_TYPE_INT, {.i64=0}, 0, 100, FLAGS, "fin_pad"},
113 { "fout_pad", "fixed output cubemap pads", OFFSET(fout_pad), AV_OPT_TYPE_INT, {.i64=0}, 0, 100, FLAGS, "fout_pad"},
114 { "yaw", "yaw rotation", OFFSET(yaw), AV_OPT_TYPE_FLOAT, {.dbl=0.f}, -180.f, 180.f, FLAGS, "yaw"},
115 { "pitch", "pitch rotation", OFFSET(pitch), AV_OPT_TYPE_FLOAT, {.dbl=0.f}, -180.f, 180.f, FLAGS, "pitch"},
116 { "roll", "roll rotation", OFFSET(roll), AV_OPT_TYPE_FLOAT, {.dbl=0.f}, -180.f, 180.f, FLAGS, "roll"},
117 { "rorder", "rotation order", OFFSET(rorder), AV_OPT_TYPE_STRING, {.str="ypr"}, 0, 0, FLAGS, "rorder"},
118 { "h_fov", "horizontal field of view", OFFSET(h_fov), AV_OPT_TYPE_FLOAT, {.dbl=90.f}, 0.00001f, 360.f, FLAGS, "h_fov"},
119 { "v_fov", "vertical field of view", OFFSET(v_fov), AV_OPT_TYPE_FLOAT, {.dbl=45.f}, 0.00001f, 360.f, FLAGS, "v_fov"},
120 { "d_fov", "diagonal field of view", OFFSET(d_fov), AV_OPT_TYPE_FLOAT, {.dbl=0.f}, 0.f, 360.f, FLAGS, "d_fov"},
121 { "h_flip", "flip out video horizontally", OFFSET(h_flip), AV_OPT_TYPE_BOOL, {.i64=0}, 0, 1, FLAGS, "h_flip"},
122 { "v_flip", "flip out video vertically", OFFSET(v_flip), AV_OPT_TYPE_BOOL, {.i64=0}, 0, 1, FLAGS, "v_flip"},
123 { "d_flip", "flip out video indepth", OFFSET(d_flip), AV_OPT_TYPE_BOOL, {.i64=0}, 0, 1, FLAGS, "d_flip"},
124 { "ih_flip", "flip in video horizontally", OFFSET(ih_flip), AV_OPT_TYPE_BOOL, {.i64=0}, 0, 1, FLAGS, "ih_flip"},
125 { "iv_flip", "flip in video vertically", OFFSET(iv_flip), AV_OPT_TYPE_BOOL, {.i64=0}, 0, 1, FLAGS, "iv_flip"},
126 { "in_trans", "transpose video input", OFFSET(in_transpose), AV_OPT_TYPE_BOOL, {.i64=0}, 0, 1, FLAGS, "in_transpose"},
127 { "out_trans", "transpose video output", OFFSET(out_transpose), AV_OPT_TYPE_BOOL, {.i64=0}, 0, 1, FLAGS, "out_transpose"},
131 AVFILTER_DEFINE_CLASS(v360);
133 static int query_formats(AVFilterContext *ctx)
135 static const enum AVPixelFormat pix_fmts[] = {
137 AV_PIX_FMT_YUVA444P, AV_PIX_FMT_YUVA444P9,
138 AV_PIX_FMT_YUVA444P10, AV_PIX_FMT_YUVA444P12,
139 AV_PIX_FMT_YUVA444P16,
142 AV_PIX_FMT_YUVA422P, AV_PIX_FMT_YUVA422P9,
143 AV_PIX_FMT_YUVA422P10, AV_PIX_FMT_YUVA422P12,
144 AV_PIX_FMT_YUVA422P16,
147 AV_PIX_FMT_YUVA420P, AV_PIX_FMT_YUVA420P9,
148 AV_PIX_FMT_YUVA420P10, AV_PIX_FMT_YUVA420P16,
151 AV_PIX_FMT_YUVJ444P, AV_PIX_FMT_YUVJ440P,
152 AV_PIX_FMT_YUVJ422P, AV_PIX_FMT_YUVJ420P,
156 AV_PIX_FMT_YUV444P, AV_PIX_FMT_YUV444P9,
157 AV_PIX_FMT_YUV444P10, AV_PIX_FMT_YUV444P12,
158 AV_PIX_FMT_YUV444P14, AV_PIX_FMT_YUV444P16,
161 AV_PIX_FMT_YUV440P, AV_PIX_FMT_YUV440P10,
162 AV_PIX_FMT_YUV440P12,
165 AV_PIX_FMT_YUV422P, AV_PIX_FMT_YUV422P9,
166 AV_PIX_FMT_YUV422P10, AV_PIX_FMT_YUV422P12,
167 AV_PIX_FMT_YUV422P14, AV_PIX_FMT_YUV422P16,
170 AV_PIX_FMT_YUV420P, AV_PIX_FMT_YUV420P9,
171 AV_PIX_FMT_YUV420P10, AV_PIX_FMT_YUV420P12,
172 AV_PIX_FMT_YUV420P14, AV_PIX_FMT_YUV420P16,
181 AV_PIX_FMT_GBRP, AV_PIX_FMT_GBRP9,
182 AV_PIX_FMT_GBRP10, AV_PIX_FMT_GBRP12,
183 AV_PIX_FMT_GBRP14, AV_PIX_FMT_GBRP16,
186 AV_PIX_FMT_GBRAP, AV_PIX_FMT_GBRAP10,
187 AV_PIX_FMT_GBRAP12, AV_PIX_FMT_GBRAP16,
190 AV_PIX_FMT_GRAY8, AV_PIX_FMT_GRAY9,
191 AV_PIX_FMT_GRAY10, AV_PIX_FMT_GRAY12,
192 AV_PIX_FMT_GRAY14, AV_PIX_FMT_GRAY16,
197 AVFilterFormats *fmts_list = ff_make_format_list(pix_fmts);
199 return AVERROR(ENOMEM);
200 return ff_set_common_formats(ctx, fmts_list);
203 #define DEFINE_REMAP1_LINE(bits, div) \
204 static void remap1_##bits##bit_line_c(uint8_t *dst, int width, const uint8_t *src, \
205 ptrdiff_t in_linesize, \
206 const uint16_t *u, const uint16_t *v, const int16_t *ker) \
208 const uint##bits##_t *s = (const uint##bits##_t *)src; \
209 uint##bits##_t *d = (uint##bits##_t *)dst; \
211 in_linesize /= div; \
213 for (int x = 0; x < width; x++) \
214 d[x] = s[v[x] * in_linesize + u[x]]; \
217 DEFINE_REMAP1_LINE( 8, 1)
218 DEFINE_REMAP1_LINE(16, 2)
221 * Generate remapping function with a given window size and pixel depth.
223 * @param ws size of interpolation window
224 * @param bits number of bits per pixel
226 #define DEFINE_REMAP(ws, bits) \
227 static int remap##ws##_##bits##bit_slice(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs) \
229 ThreadData *td = arg; \
230 const V360Context *s = ctx->priv; \
231 const AVFrame *in = td->in; \
232 AVFrame *out = td->out; \
234 for (int stereo = 0; stereo < 1 + s->out_stereo > STEREO_2D; stereo++) { \
235 for (int plane = 0; plane < s->nb_planes; plane++) { \
236 const int in_linesize = in->linesize[plane]; \
237 const int out_linesize = out->linesize[plane]; \
238 const int uv_linesize = s->uv_linesize[plane]; \
239 const int in_offset_w = stereo ? s->in_offset_w[plane] : 0; \
240 const int in_offset_h = stereo ? s->in_offset_h[plane] : 0; \
241 const int out_offset_w = stereo ? s->out_offset_w[plane] : 0; \
242 const int out_offset_h = stereo ? s->out_offset_h[plane] : 0; \
243 const uint8_t *src = in->data[plane] + in_offset_h * in_linesize + in_offset_w * (bits >> 3); \
244 uint8_t *dst = out->data[plane] + out_offset_h * out_linesize + out_offset_w * (bits >> 3); \
245 const int width = s->pr_width[plane]; \
246 const int height = s->pr_height[plane]; \
248 const int slice_start = (height * jobnr ) / nb_jobs; \
249 const int slice_end = (height * (jobnr + 1)) / nb_jobs; \
251 for (int y = slice_start; y < slice_end; y++) { \
252 const unsigned map = s->map[plane]; \
253 const uint16_t *u = s->u[map] + y * uv_linesize * ws * ws; \
254 const uint16_t *v = s->v[map] + y * uv_linesize * ws * ws; \
255 const int16_t *ker = s->ker[map] + y * uv_linesize * ws * ws; \
257 s->remap_line(dst + y * out_linesize, width, src, in_linesize, u, v, ker); \
272 #define DEFINE_REMAP_LINE(ws, bits, div) \
273 static void remap##ws##_##bits##bit_line_c(uint8_t *dst, int width, const uint8_t *src, \
274 ptrdiff_t in_linesize, \
275 const uint16_t *u, const uint16_t *v, const int16_t *ker) \
277 const uint##bits##_t *s = (const uint##bits##_t *)src; \
278 uint##bits##_t *d = (uint##bits##_t *)dst; \
280 in_linesize /= div; \
282 for (int x = 0; x < width; x++) { \
283 const uint16_t *uu = u + x * ws * ws; \
284 const uint16_t *vv = v + x * ws * ws; \
285 const int16_t *kker = ker + x * ws * ws; \
288 for (int i = 0; i < ws; i++) { \
289 for (int j = 0; j < ws; j++) { \
290 tmp += kker[i * ws + j] * s[vv[i * ws + j] * in_linesize + uu[i * ws + j]]; \
294 d[x] = av_clip_uint##bits(tmp >> 14); \
298 DEFINE_REMAP_LINE(2, 8, 1)
299 DEFINE_REMAP_LINE(4, 8, 1)
300 DEFINE_REMAP_LINE(2, 16, 2)
301 DEFINE_REMAP_LINE(4, 16, 2)
303 void ff_v360_init(V360Context *s, int depth)
307 s->remap_line = depth <= 8 ? remap1_8bit_line_c : remap1_16bit_line_c;
310 s->remap_line = depth <= 8 ? remap2_8bit_line_c : remap2_16bit_line_c;
314 s->remap_line = depth <= 8 ? remap4_8bit_line_c : remap4_16bit_line_c;
319 ff_v360_init_x86(s, depth);
323 * Save nearest pixel coordinates for remapping.
325 * @param du horizontal relative coordinate
326 * @param dv vertical relative coordinate
327 * @param rmap calculated 4x4 window
328 * @param u u remap data
329 * @param v v remap data
330 * @param ker ker remap data
332 static void nearest_kernel(float du, float dv, const XYRemap *rmap,
333 uint16_t *u, uint16_t *v, int16_t *ker)
335 const int i = roundf(dv) + 1;
336 const int j = roundf(du) + 1;
338 u[0] = rmap->u[i][j];
339 v[0] = rmap->v[i][j];
343 * Calculate kernel for bilinear interpolation.
345 * @param du horizontal relative coordinate
346 * @param dv vertical relative coordinate
347 * @param rmap calculated 4x4 window
348 * @param u u remap data
349 * @param v v remap data
350 * @param ker ker remap data
352 static void bilinear_kernel(float du, float dv, const XYRemap *rmap,
353 uint16_t *u, uint16_t *v, int16_t *ker)
355 for (int i = 0; i < 2; i++) {
356 for (int j = 0; j < 2; j++) {
357 u[i * 2 + j] = rmap->u[i + 1][j + 1];
358 v[i * 2 + j] = rmap->v[i + 1][j + 1];
362 ker[0] = lrintf((1.f - du) * (1.f - dv) * 16385.f);
363 ker[1] = lrintf( du * (1.f - dv) * 16385.f);
364 ker[2] = lrintf((1.f - du) * dv * 16385.f);
365 ker[3] = lrintf( du * dv * 16385.f);
369 * Calculate 1-dimensional cubic coefficients.
371 * @param t relative coordinate
372 * @param coeffs coefficients
374 static inline void calculate_bicubic_coeffs(float t, float *coeffs)
376 const float tt = t * t;
377 const float ttt = t * t * t;
379 coeffs[0] = - t / 3.f + tt / 2.f - ttt / 6.f;
380 coeffs[1] = 1.f - t / 2.f - tt + ttt / 2.f;
381 coeffs[2] = t + tt / 2.f - ttt / 2.f;
382 coeffs[3] = - t / 6.f + ttt / 6.f;
386 * Calculate kernel for bicubic interpolation.
388 * @param du horizontal relative coordinate
389 * @param dv vertical relative coordinate
390 * @param rmap calculated 4x4 window
391 * @param u u remap data
392 * @param v v remap data
393 * @param ker ker remap data
395 static void bicubic_kernel(float du, float dv, const XYRemap *rmap,
396 uint16_t *u, uint16_t *v, int16_t *ker)
401 calculate_bicubic_coeffs(du, du_coeffs);
402 calculate_bicubic_coeffs(dv, dv_coeffs);
404 for (int i = 0; i < 4; i++) {
405 for (int j = 0; j < 4; j++) {
406 u[i * 4 + j] = rmap->u[i][j];
407 v[i * 4 + j] = rmap->v[i][j];
408 ker[i * 4 + j] = lrintf(du_coeffs[j] * dv_coeffs[i] * 16385.f);
414 * Calculate 1-dimensional lanczos coefficients.
416 * @param t relative coordinate
417 * @param coeffs coefficients
419 static inline void calculate_lanczos_coeffs(float t, float *coeffs)
423 for (int i = 0; i < 4; i++) {
424 const float x = M_PI * (t - i + 1);
428 coeffs[i] = sinf(x) * sinf(x / 2.f) / (x * x / 2.f);
433 for (int i = 0; i < 4; i++) {
439 * Calculate kernel for lanczos interpolation.
441 * @param du horizontal relative coordinate
442 * @param dv vertical relative coordinate
443 * @param rmap calculated 4x4 window
444 * @param u u remap data
445 * @param v v remap data
446 * @param ker ker remap data
448 static void lanczos_kernel(float du, float dv, const XYRemap *rmap,
449 uint16_t *u, uint16_t *v, int16_t *ker)
454 calculate_lanczos_coeffs(du, du_coeffs);
455 calculate_lanczos_coeffs(dv, dv_coeffs);
457 for (int i = 0; i < 4; i++) {
458 for (int j = 0; j < 4; j++) {
459 u[i * 4 + j] = rmap->u[i][j];
460 v[i * 4 + j] = rmap->v[i][j];
461 ker[i * 4 + j] = lrintf(du_coeffs[j] * dv_coeffs[i] * 16385.f);
467 * Modulo operation with only positive remainders.
472 * @return positive remainder of (a / b)
474 static inline int mod(int a, int b)
476 const int res = a % b;
485 * Convert char to corresponding direction.
486 * Used for cubemap options.
488 static int get_direction(char c)
509 * Convert char to corresponding rotation angle.
510 * Used for cubemap options.
512 static int get_rotation(char c)
529 * Convert char to corresponding rotation order.
531 static int get_rorder(char c)
549 * Prepare data for processing cubemap input format.
551 * @param ctx filter context
555 static int prepare_cube_in(AVFilterContext *ctx)
557 V360Context *s = ctx->priv;
559 for (int face = 0; face < NB_FACES; face++) {
560 const char c = s->in_forder[face];
564 av_log(ctx, AV_LOG_ERROR,
565 "Incomplete in_forder option. Direction for all 6 faces should be specified.\n");
566 return AVERROR(EINVAL);
569 direction = get_direction(c);
570 if (direction == -1) {
571 av_log(ctx, AV_LOG_ERROR,
572 "Incorrect direction symbol '%c' in in_forder option.\n", c);
573 return AVERROR(EINVAL);
576 s->in_cubemap_face_order[direction] = face;
579 for (int face = 0; face < NB_FACES; face++) {
580 const char c = s->in_frot[face];
584 av_log(ctx, AV_LOG_ERROR,
585 "Incomplete in_frot option. Rotation for all 6 faces should be specified.\n");
586 return AVERROR(EINVAL);
589 rotation = get_rotation(c);
590 if (rotation == -1) {
591 av_log(ctx, AV_LOG_ERROR,
592 "Incorrect rotation symbol '%c' in in_frot option.\n", c);
593 return AVERROR(EINVAL);
596 s->in_cubemap_face_rotation[face] = rotation;
603 * Prepare data for processing cubemap output format.
605 * @param ctx filter context
609 static int prepare_cube_out(AVFilterContext *ctx)
611 V360Context *s = ctx->priv;
613 for (int face = 0; face < NB_FACES; face++) {
614 const char c = s->out_forder[face];
618 av_log(ctx, AV_LOG_ERROR,
619 "Incomplete out_forder option. Direction for all 6 faces should be specified.\n");
620 return AVERROR(EINVAL);
623 direction = get_direction(c);
624 if (direction == -1) {
625 av_log(ctx, AV_LOG_ERROR,
626 "Incorrect direction symbol '%c' in out_forder option.\n", c);
627 return AVERROR(EINVAL);
630 s->out_cubemap_direction_order[face] = direction;
633 for (int face = 0; face < NB_FACES; face++) {
634 const char c = s->out_frot[face];
638 av_log(ctx, AV_LOG_ERROR,
639 "Incomplete out_frot option. Rotation for all 6 faces should be specified.\n");
640 return AVERROR(EINVAL);
643 rotation = get_rotation(c);
644 if (rotation == -1) {
645 av_log(ctx, AV_LOG_ERROR,
646 "Incorrect rotation symbol '%c' in out_frot option.\n", c);
647 return AVERROR(EINVAL);
650 s->out_cubemap_face_rotation[face] = rotation;
656 static inline void rotate_cube_face(float *uf, float *vf, int rotation)
682 static inline void rotate_cube_face_inverse(float *uf, float *vf, int rotation)
713 static void normalize_vector(float *vec)
715 const float norm = sqrtf(vec[0] * vec[0] + vec[1] * vec[1] + vec[2] * vec[2]);
723 * Calculate 3D coordinates on sphere for corresponding cubemap position.
724 * Common operation for every cubemap.
726 * @param s filter private context
727 * @param uf horizontal cubemap coordinate [0, 1)
728 * @param vf vertical cubemap coordinate [0, 1)
729 * @param face face of cubemap
730 * @param vec coordinates on sphere
731 * @param scalew scale for uf
732 * @param scaleh scale for vf
734 static void cube_to_xyz(const V360Context *s,
735 float uf, float vf, int face,
736 float *vec, float scalew, float scaleh)
738 const int direction = s->out_cubemap_direction_order[face];
744 rotate_cube_face_inverse(&uf, &vf, s->out_cubemap_face_rotation[face]);
785 normalize_vector(vec);
789 * Calculate cubemap position for corresponding 3D coordinates on sphere.
790 * Common operation for every cubemap.
792 * @param s filter private context
793 * @param vec coordinated on sphere
794 * @param uf horizontal cubemap coordinate [0, 1)
795 * @param vf vertical cubemap coordinate [0, 1)
796 * @param direction direction of view
798 static void xyz_to_cube(const V360Context *s,
800 float *uf, float *vf, int *direction)
802 const float phi = atan2f(vec[0], -vec[2]);
803 const float theta = asinf(-vec[1]);
804 float phi_norm, theta_threshold;
807 if (phi >= -M_PI_4 && phi < M_PI_4) {
810 } else if (phi >= -(M_PI_2 + M_PI_4) && phi < -M_PI_4) {
812 phi_norm = phi + M_PI_2;
813 } else if (phi >= M_PI_4 && phi < M_PI_2 + M_PI_4) {
815 phi_norm = phi - M_PI_2;
818 phi_norm = phi + ((phi > 0.f) ? -M_PI : M_PI);
821 theta_threshold = atanf(cosf(phi_norm));
822 if (theta > theta_threshold) {
824 } else if (theta < -theta_threshold) {
828 switch (*direction) {
830 *uf = vec[2] / vec[0];
831 *vf = -vec[1] / vec[0];
834 *uf = vec[2] / vec[0];
835 *vf = vec[1] / vec[0];
838 *uf = vec[0] / vec[1];
839 *vf = -vec[2] / vec[1];
842 *uf = -vec[0] / vec[1];
843 *vf = -vec[2] / vec[1];
846 *uf = -vec[0] / vec[2];
847 *vf = vec[1] / vec[2];
850 *uf = -vec[0] / vec[2];
851 *vf = -vec[1] / vec[2];
857 face = s->in_cubemap_face_order[*direction];
858 rotate_cube_face(uf, vf, s->in_cubemap_face_rotation[face]);
860 (*uf) *= s->input_mirror_modifier[0];
861 (*vf) *= s->input_mirror_modifier[1];
865 * Find position on another cube face in case of overflow/underflow.
866 * Used for calculation of interpolation window.
868 * @param s filter private context
869 * @param uf horizontal cubemap coordinate
870 * @param vf vertical cubemap coordinate
871 * @param direction direction of view
872 * @param new_uf new horizontal cubemap coordinate
873 * @param new_vf new vertical cubemap coordinate
874 * @param face face position on cubemap
876 static void process_cube_coordinates(const V360Context *s,
877 float uf, float vf, int direction,
878 float *new_uf, float *new_vf, int *face)
881 * Cubemap orientation
888 * +-------+-------+-------+-------+ ^ e |
890 * | left | front | right | back | | g |
891 * +-------+-------+-------+-------+ v h v
897 *face = s->in_cubemap_face_order[direction];
898 rotate_cube_face_inverse(&uf, &vf, s->in_cubemap_face_rotation[*face]);
900 if ((uf < -1.f || uf >= 1.f) && (vf < -1.f || vf >= 1.f)) {
901 // There are no pixels to use in this case
904 } else if (uf < -1.f) {
940 } else if (uf >= 1.f) {
976 } else if (vf < -1.f) {
1012 } else if (vf >= 1.f) {
1014 switch (direction) {
1054 *face = s->in_cubemap_face_order[direction];
1055 rotate_cube_face(new_uf, new_vf, s->in_cubemap_face_rotation[*face]);
1059 * Calculate 3D coordinates on sphere for corresponding frame position in cubemap3x2 format.
1061 * @param s filter private context
1062 * @param i horizontal position on frame [0, width)
1063 * @param j vertical position on frame [0, height)
1064 * @param width frame width
1065 * @param height frame height
1066 * @param vec coordinates on sphere
1068 static void cube3x2_to_xyz(const V360Context *s,
1069 int i, int j, int width, int height,
1072 const float scalew = s->fout_pad > 0 ? 1.f - s->fout_pad / (s->out_width / 3.f) : 1.f - s->out_pad;
1073 const float scaleh = s->fout_pad > 0 ? 1.f - s->fout_pad / (s->out_height / 2.f) : 1.f - s->out_pad;
1075 const float ew = width / 3.f;
1076 const float eh = height / 2.f;
1078 const int u_face = floorf(i / ew);
1079 const int v_face = floorf(j / eh);
1080 const int face = u_face + 3 * v_face;
1082 const int u_shift = ceilf(ew * u_face);
1083 const int v_shift = ceilf(eh * v_face);
1084 const int ewi = ceilf(ew * (u_face + 1)) - u_shift;
1085 const int ehi = ceilf(eh * (v_face + 1)) - v_shift;
1087 const float uf = 2.f * (i - u_shift + 0.5f) / ewi - 1.f;
1088 const float vf = 2.f * (j - v_shift + 0.5f) / ehi - 1.f;
1090 cube_to_xyz(s, uf, vf, face, vec, scalew, scaleh);
1094 * Calculate frame position in cubemap3x2 format for corresponding 3D coordinates on sphere.
1096 * @param s filter private context
1097 * @param vec coordinates on sphere
1098 * @param width frame width
1099 * @param height frame height
1100 * @param us horizontal coordinates for interpolation window
1101 * @param vs vertical coordinates for interpolation window
1102 * @param du horizontal relative coordinate
1103 * @param dv vertical relative coordinate
1105 static void xyz_to_cube3x2(const V360Context *s,
1106 const float *vec, int width, int height,
1107 uint16_t us[4][4], uint16_t vs[4][4], float *du, float *dv)
1109 const float scalew = s->fin_pad > 0 ? 1.f - s->fin_pad / (s->in_width / 3.f) : 1.f - s->in_pad;
1110 const float scaleh = s->fin_pad > 0 ? 1.f - s->fin_pad / (s->in_height / 2.f) : 1.f - s->in_pad;
1111 const float ew = width / 3.f;
1112 const float eh = height / 2.f;
1116 int direction, face;
1119 xyz_to_cube(s, vec, &uf, &vf, &direction);
1124 face = s->in_cubemap_face_order[direction];
1127 ewi = ceilf(ew * (u_face + 1)) - ceilf(ew * u_face);
1128 ehi = ceilf(eh * (v_face + 1)) - ceilf(eh * v_face);
1130 uf = 0.5f * ewi * (uf + 1.f) - 0.5f;
1131 vf = 0.5f * ehi * (vf + 1.f) - 0.5f;
1139 for (int i = -1; i < 3; i++) {
1140 for (int j = -1; j < 3; j++) {
1141 int new_ui = ui + j;
1142 int new_vi = vi + i;
1143 int u_shift, v_shift;
1144 int new_ewi, new_ehi;
1146 if (new_ui >= 0 && new_ui < ewi && new_vi >= 0 && new_vi < ehi) {
1147 face = s->in_cubemap_face_order[direction];
1151 u_shift = ceilf(ew * u_face);
1152 v_shift = ceilf(eh * v_face);
1154 uf = 2.f * new_ui / ewi - 1.f;
1155 vf = 2.f * new_vi / ehi - 1.f;
1160 process_cube_coordinates(s, uf, vf, direction, &uf, &vf, &face);
1167 u_shift = ceilf(ew * u_face);
1168 v_shift = ceilf(eh * v_face);
1169 new_ewi = ceilf(ew * (u_face + 1)) - u_shift;
1170 new_ehi = ceilf(eh * (v_face + 1)) - v_shift;
1172 new_ui = av_clip(roundf(0.5f * new_ewi * (uf + 1.f)), 0, new_ewi - 1);
1173 new_vi = av_clip(roundf(0.5f * new_ehi * (vf + 1.f)), 0, new_ehi - 1);
1176 us[i + 1][j + 1] = u_shift + new_ui;
1177 vs[i + 1][j + 1] = v_shift + new_vi;
1183 * Calculate 3D coordinates on sphere for corresponding frame position in cubemap1x6 format.
1185 * @param s filter private context
1186 * @param i horizontal position on frame [0, width)
1187 * @param j vertical position on frame [0, height)
1188 * @param width frame width
1189 * @param height frame height
1190 * @param vec coordinates on sphere
1192 static void cube1x6_to_xyz(const V360Context *s,
1193 int i, int j, int width, int height,
1196 const float scalew = s->fout_pad > 0 ? 1.f - (float)(s->fout_pad) / s->out_width : 1.f - s->out_pad;
1197 const float scaleh = s->fout_pad > 0 ? 1.f - s->fout_pad / (s->out_height / 6.f) : 1.f - s->out_pad;
1199 const float ew = width;
1200 const float eh = height / 6.f;
1202 const int face = floorf(j / eh);
1204 const int v_shift = ceilf(eh * face);
1205 const int ehi = ceilf(eh * (face + 1)) - v_shift;
1207 const float uf = 2.f * (i + 0.5f) / ew - 1.f;
1208 const float vf = 2.f * (j - v_shift + 0.5f) / ehi - 1.f;
1210 cube_to_xyz(s, uf, vf, face, vec, scalew, scaleh);
1214 * Calculate 3D coordinates on sphere for corresponding frame position in cubemap6x1 format.
1216 * @param s filter private context
1217 * @param i horizontal position on frame [0, width)
1218 * @param j vertical position on frame [0, height)
1219 * @param width frame width
1220 * @param height frame height
1221 * @param vec coordinates on sphere
1223 static void cube6x1_to_xyz(const V360Context *s,
1224 int i, int j, int width, int height,
1227 const float scalew = s->fout_pad > 0 ? 1.f - s->fout_pad / (s->out_width / 6.f) : 1.f - s->out_pad;
1228 const float scaleh = s->fout_pad > 0 ? 1.f - (float)(s->fout_pad) / s->out_height : 1.f - s->out_pad;
1230 const float ew = width / 6.f;
1231 const float eh = height;
1233 const int face = floorf(i / ew);
1235 const int u_shift = ceilf(ew * face);
1236 const int ewi = ceilf(ew * (face + 1)) - u_shift;
1238 const float uf = 2.f * (i - u_shift + 0.5f) / ewi - 1.f;
1239 const float vf = 2.f * (j + 0.5f) / eh - 1.f;
1241 cube_to_xyz(s, uf, vf, face, vec, scalew, scaleh);
1245 * Calculate frame position in cubemap1x6 format for corresponding 3D coordinates on sphere.
1247 * @param s filter private context
1248 * @param vec coordinates on sphere
1249 * @param width frame width
1250 * @param height frame height
1251 * @param us horizontal coordinates for interpolation window
1252 * @param vs vertical coordinates for interpolation window
1253 * @param du horizontal relative coordinate
1254 * @param dv vertical relative coordinate
1256 static void xyz_to_cube1x6(const V360Context *s,
1257 const float *vec, int width, int height,
1258 uint16_t us[4][4], uint16_t vs[4][4], float *du, float *dv)
1260 const float scalew = s->fin_pad > 0 ? 1.f - (float)(s->fin_pad) / s->in_width : 1.f - s->in_pad;
1261 const float scaleh = s->fin_pad > 0 ? 1.f - s->fin_pad / (s->in_height / 6.f) : 1.f - s->in_pad;
1262 const float eh = height / 6.f;
1263 const int ewi = width;
1267 int direction, face;
1269 xyz_to_cube(s, vec, &uf, &vf, &direction);
1274 face = s->in_cubemap_face_order[direction];
1275 ehi = ceilf(eh * (face + 1)) - ceilf(eh * face);
1277 uf = 0.5f * ewi * (uf + 1.f) - 0.5f;
1278 vf = 0.5f * ehi * (vf + 1.f) - 0.5f;
1286 for (int i = -1; i < 3; i++) {
1287 for (int j = -1; j < 3; j++) {
1288 int new_ui = ui + j;
1289 int new_vi = vi + i;
1293 if (new_ui >= 0 && new_ui < ewi && new_vi >= 0 && new_vi < ehi) {
1294 face = s->in_cubemap_face_order[direction];
1296 v_shift = ceilf(eh * face);
1298 uf = 2.f * new_ui / ewi - 1.f;
1299 vf = 2.f * new_vi / ehi - 1.f;
1304 process_cube_coordinates(s, uf, vf, direction, &uf, &vf, &face);
1309 v_shift = ceilf(eh * face);
1310 new_ehi = ceilf(eh * (face + 1)) - v_shift;
1312 new_ui = av_clip(roundf(0.5f * ewi * (uf + 1.f)), 0, ewi - 1);
1313 new_vi = av_clip(roundf(0.5f * new_ehi * (vf + 1.f)), 0, new_ehi - 1);
1316 us[i + 1][j + 1] = new_ui;
1317 vs[i + 1][j + 1] = v_shift + new_vi;
1323 * Calculate frame position in cubemap6x1 format for corresponding 3D coordinates on sphere.
1325 * @param s filter private context
1326 * @param vec coordinates on sphere
1327 * @param width frame width
1328 * @param height frame height
1329 * @param us horizontal coordinates for interpolation window
1330 * @param vs vertical coordinates for interpolation window
1331 * @param du horizontal relative coordinate
1332 * @param dv vertical relative coordinate
1334 static void xyz_to_cube6x1(const V360Context *s,
1335 const float *vec, int width, int height,
1336 uint16_t us[4][4], uint16_t vs[4][4], float *du, float *dv)
1338 const float scalew = s->fin_pad > 0 ? 1.f - s->fin_pad / (s->in_width / 6.f) : 1.f - s->in_pad;
1339 const float scaleh = s->fin_pad > 0 ? 1.f - (float)(s->fin_pad) / s->in_height : 1.f - s->in_pad;
1340 const float ew = width / 6.f;
1341 const int ehi = height;
1345 int direction, face;
1347 xyz_to_cube(s, vec, &uf, &vf, &direction);
1352 face = s->in_cubemap_face_order[direction];
1353 ewi = ceilf(ew * (face + 1)) - ceilf(ew * face);
1355 uf = 0.5f * ewi * (uf + 1.f) - 0.5f;
1356 vf = 0.5f * ehi * (vf + 1.f) - 0.5f;
1364 for (int i = -1; i < 3; i++) {
1365 for (int j = -1; j < 3; j++) {
1366 int new_ui = ui + j;
1367 int new_vi = vi + i;
1371 if (new_ui >= 0 && new_ui < ewi && new_vi >= 0 && new_vi < ehi) {
1372 face = s->in_cubemap_face_order[direction];
1374 u_shift = ceilf(ew * face);
1376 uf = 2.f * new_ui / ewi - 1.f;
1377 vf = 2.f * new_vi / ehi - 1.f;
1382 process_cube_coordinates(s, uf, vf, direction, &uf, &vf, &face);
1387 u_shift = ceilf(ew * face);
1388 new_ewi = ceilf(ew * (face + 1)) - u_shift;
1390 new_ui = av_clip(roundf(0.5f * new_ewi * (uf + 1.f)), 0, new_ewi - 1);
1391 new_vi = av_clip(roundf(0.5f * ehi * (vf + 1.f)), 0, ehi - 1);
1394 us[i + 1][j + 1] = u_shift + new_ui;
1395 vs[i + 1][j + 1] = new_vi;
1401 * Calculate 3D coordinates on sphere for corresponding frame position in equirectangular format.
1403 * @param s filter private context
1404 * @param i horizontal position on frame [0, width)
1405 * @param j vertical position on frame [0, height)
1406 * @param width frame width
1407 * @param height frame height
1408 * @param vec coordinates on sphere
1410 static void equirect_to_xyz(const V360Context *s,
1411 int i, int j, int width, int height,
1414 const float phi = ((2.f * i) / width - 1.f) * M_PI;
1415 const float theta = ((2.f * j) / height - 1.f) * M_PI_2;
1417 const float sin_phi = sinf(phi);
1418 const float cos_phi = cosf(phi);
1419 const float sin_theta = sinf(theta);
1420 const float cos_theta = cosf(theta);
1422 vec[0] = cos_theta * sin_phi;
1423 vec[1] = -sin_theta;
1424 vec[2] = -cos_theta * cos_phi;
1428 * Prepare data for processing stereographic output format.
1430 * @param ctx filter context
1432 * @return error code
1434 static int prepare_stereographic_out(AVFilterContext *ctx)
1436 V360Context *s = ctx->priv;
1438 s->flat_range[0] = tanf(FFMIN(s->h_fov, 359.f) * M_PI / 720.f);
1439 s->flat_range[1] = tanf(FFMIN(s->v_fov, 359.f) * M_PI / 720.f);
1445 * Calculate 3D coordinates on sphere for corresponding frame position in stereographic format.
1447 * @param s filter private context
1448 * @param i horizontal position on frame [0, width)
1449 * @param j vertical position on frame [0, height)
1450 * @param width frame width
1451 * @param height frame height
1452 * @param vec coordinates on sphere
1454 static void stereographic_to_xyz(const V360Context *s,
1455 int i, int j, int width, int height,
1458 const float x = ((2.f * i) / width - 1.f) * s->flat_range[0];
1459 const float y = ((2.f * j) / height - 1.f) * s->flat_range[1];
1460 const float xy = x * x + y * y;
1462 vec[0] = 2.f * x / (1.f + xy);
1463 vec[1] = (-1.f + xy) / (1.f + xy);
1464 vec[2] = 2.f * y / (1.f + xy);
1466 normalize_vector(vec);
1470 * Calculate frame position in stereographic format for corresponding 3D coordinates on sphere.
1472 * @param s filter private context
1473 * @param vec coordinates on sphere
1474 * @param width frame width
1475 * @param height frame height
1476 * @param us horizontal coordinates for interpolation window
1477 * @param vs vertical coordinates for interpolation window
1478 * @param du horizontal relative coordinate
1479 * @param dv vertical relative coordinate
1481 static void xyz_to_stereographic(const V360Context *s,
1482 const float *vec, int width, int height,
1483 uint16_t us[4][4], uint16_t vs[4][4], float *du, float *dv)
1485 const float x = av_clipf(vec[0] / (1.f - vec[1]), -1.f, 1.f) * s->input_mirror_modifier[0];
1486 const float y = av_clipf(vec[2] / (1.f - vec[1]), -1.f, 1.f) * s->input_mirror_modifier[1];
1490 uf = (x + 1.f) * width / 2.f;
1491 vf = (y + 1.f) * height / 2.f;
1498 for (int i = -1; i < 3; i++) {
1499 for (int j = -1; j < 3; j++) {
1500 us[i + 1][j + 1] = av_clip(ui + j, 0, width - 1);
1501 vs[i + 1][j + 1] = av_clip(vi + i, 0, height - 1);
1507 * Calculate frame position in equirectangular format for corresponding 3D coordinates on sphere.
1509 * @param s filter private context
1510 * @param vec coordinates on sphere
1511 * @param width frame width
1512 * @param height frame height
1513 * @param us horizontal coordinates for interpolation window
1514 * @param vs vertical coordinates for interpolation window
1515 * @param du horizontal relative coordinate
1516 * @param dv vertical relative coordinate
1518 static void xyz_to_equirect(const V360Context *s,
1519 const float *vec, int width, int height,
1520 uint16_t us[4][4], uint16_t vs[4][4], float *du, float *dv)
1522 const float phi = atan2f(vec[0], -vec[2]) * s->input_mirror_modifier[0];
1523 const float theta = asinf(-vec[1]) * s->input_mirror_modifier[1];
1527 uf = (phi / M_PI + 1.f) * width / 2.f;
1528 vf = (theta / M_PI_2 + 1.f) * height / 2.f;
1535 for (int i = -1; i < 3; i++) {
1536 for (int j = -1; j < 3; j++) {
1537 us[i + 1][j + 1] = mod(ui + j, width);
1538 vs[i + 1][j + 1] = av_clip(vi + i, 0, height - 1);
1544 * Calculate frame position in mercator format for corresponding 3D coordinates on sphere.
1546 * @param s filter private context
1547 * @param vec coordinates on sphere
1548 * @param width frame width
1549 * @param height frame height
1550 * @param us horizontal coordinates for interpolation window
1551 * @param vs vertical coordinates for interpolation window
1552 * @param du horizontal relative coordinate
1553 * @param dv vertical relative coordinate
1555 static void xyz_to_mercator(const V360Context *s,
1556 const float *vec, int width, int height,
1557 uint16_t us[4][4], uint16_t vs[4][4], float *du, float *dv)
1559 const float phi = atan2f(vec[0], -vec[2]) * s->input_mirror_modifier[0];
1560 const float theta = -vec[1] * s->input_mirror_modifier[1];
1564 uf = (phi / M_PI + 1.f) * width / 2.f;
1565 vf = (av_clipf(logf((1.f + theta) / (1.f - theta)) / (2.f * M_PI), -1.f, 1.f) + 1.f) * height / 2.f;
1572 for (int i = -1; i < 3; i++) {
1573 for (int j = -1; j < 3; j++) {
1574 us[i + 1][j + 1] = av_clip(ui + j, 0, width - 1);
1575 vs[i + 1][j + 1] = av_clip(vi + i, 0, height - 1);
1581 * Calculate 3D coordinates on sphere for corresponding frame position in mercator format.
1583 * @param s filter private context
1584 * @param i horizontal position on frame [0, width)
1585 * @param j vertical position on frame [0, height)
1586 * @param width frame width
1587 * @param height frame height
1588 * @param vec coordinates on sphere
1590 static void mercator_to_xyz(const V360Context *s,
1591 int i, int j, int width, int height,
1594 const float phi = ((2.f * i) / width - 1.f) * M_PI + M_PI_2;
1595 const float y = ((2.f * j) / height - 1.f) * M_PI;
1596 const float div = expf(2.f * y) + 1.f;
1598 const float sin_phi = sinf(phi);
1599 const float cos_phi = cosf(phi);
1600 const float sin_theta = -2.f * expf(y) / div;
1601 const float cos_theta = -(expf(2.f * y) - 1.f) / div;
1603 vec[0] = sin_theta * cos_phi;
1605 vec[2] = sin_theta * sin_phi;
1609 * Calculate frame position in ball format for corresponding 3D coordinates on sphere.
1611 * @param s filter private context
1612 * @param vec coordinates on sphere
1613 * @param width frame width
1614 * @param height frame height
1615 * @param us horizontal coordinates for interpolation window
1616 * @param vs vertical coordinates for interpolation window
1617 * @param du horizontal relative coordinate
1618 * @param dv vertical relative coordinate
1620 static void xyz_to_ball(const V360Context *s,
1621 const float *vec, int width, int height,
1622 uint16_t us[4][4], uint16_t vs[4][4], float *du, float *dv)
1624 const float l = hypotf(vec[0], vec[1]);
1625 const float r = sqrtf(1.f + vec[2]) / M_SQRT2;
1629 uf = (1.f + r * vec[0] * s->input_mirror_modifier[0] / (l > 0.f ? l : 1.f)) * width * 0.5f;
1630 vf = (1.f - r * vec[1] * s->input_mirror_modifier[1] / (l > 0.f ? l : 1.f)) * height * 0.5f;
1638 for (int i = -1; i < 3; i++) {
1639 for (int j = -1; j < 3; j++) {
1640 us[i + 1][j + 1] = av_clip(ui + j, 0, width - 1);
1641 vs[i + 1][j + 1] = av_clip(vi + i, 0, height - 1);
1647 * Calculate 3D coordinates on sphere for corresponding frame position in ball format.
1649 * @param s filter private context
1650 * @param i horizontal position on frame [0, width)
1651 * @param j vertical position on frame [0, height)
1652 * @param width frame width
1653 * @param height frame height
1654 * @param vec coordinates on sphere
1656 static void ball_to_xyz(const V360Context *s,
1657 int i, int j, int width, int height,
1660 const float x = (2.f * i) / width - 1.f;
1661 const float y = (2.f * j) / height - 1.f;
1662 const float l = hypotf(x, y);
1665 const float z = 2.f * l * sqrtf(1.f - l * l);
1667 vec[0] = z * x / (l > 0.f ? l : 1.f);
1668 vec[1] = -z * y / (l > 0.f ? l : 1.f);
1669 vec[2] = -1.f + 2.f * l * l;
1678 * Calculate 3D coordinates on sphere for corresponding frame position in hammer format.
1680 * @param s filter private context
1681 * @param i horizontal position on frame [0, width)
1682 * @param j vertical position on frame [0, height)
1683 * @param width frame width
1684 * @param height frame height
1685 * @param vec coordinates on sphere
1687 static void hammer_to_xyz(const V360Context *s,
1688 int i, int j, int width, int height,
1691 const float x = ((2.f * i) / width - 1.f);
1692 const float y = ((2.f * j) / height - 1.f);
1694 const float xx = x * x;
1695 const float yy = y * y;
1697 const float z = sqrtf(1.f - xx * 0.5f - yy * 0.5f);
1699 const float a = M_SQRT2 * x * z;
1700 const float b = 2.f * z * z - 1.f;
1702 const float aa = a * a;
1703 const float bb = b * b;
1705 const float w = sqrtf(1.f - 2.f * yy * z * z);
1707 vec[0] = w * 2.f * a * b / (aa + bb);
1708 vec[1] = -M_SQRT2 * y * z;
1709 vec[2] = -w * (bb - aa) / (aa + bb);
1711 normalize_vector(vec);
1715 * Calculate frame position in hammer format for corresponding 3D coordinates on sphere.
1717 * @param s filter private context
1718 * @param vec coordinates on sphere
1719 * @param width frame width
1720 * @param height frame height
1721 * @param us horizontal coordinates for interpolation window
1722 * @param vs vertical coordinates for interpolation window
1723 * @param du horizontal relative coordinate
1724 * @param dv vertical relative coordinate
1726 static void xyz_to_hammer(const V360Context *s,
1727 const float *vec, int width, int height,
1728 uint16_t us[4][4], uint16_t vs[4][4], float *du, float *dv)
1730 const float theta = atan2f(vec[0], -vec[2]) * s->input_mirror_modifier[0];
1732 const float z = sqrtf(1.f + sqrtf(1.f - vec[1] * vec[1]) * cosf(theta * 0.5f));
1733 const float x = sqrtf(1.f - vec[1] * vec[1]) * sinf(theta * 0.5f) / z;
1734 const float y = -vec[1] / z * s->input_mirror_modifier[1];
1738 uf = (x + 1.f) * width / 2.f;
1739 vf = (y + 1.f) * height / 2.f;
1746 for (int i = -1; i < 3; i++) {
1747 for (int j = -1; j < 3; j++) {
1748 us[i + 1][j + 1] = av_clip(ui + j, 0, width - 1);
1749 vs[i + 1][j + 1] = av_clip(vi + i, 0, height - 1);
1755 * Calculate 3D coordinates on sphere for corresponding frame position in sinusoidal format.
1757 * @param s filter private context
1758 * @param i horizontal position on frame [0, width)
1759 * @param j vertical position on frame [0, height)
1760 * @param width frame width
1761 * @param height frame height
1762 * @param vec coordinates on sphere
1764 static void sinusoidal_to_xyz(const V360Context *s,
1765 int i, int j, int width, int height,
1768 const float theta = ((2.f * j) / height - 1.f) * M_PI_2;
1769 const float phi = ((2.f * i) / width - 1.f) * M_PI / cosf(theta);
1771 const float sin_phi = sinf(phi);
1772 const float cos_phi = cosf(phi);
1773 const float sin_theta = sinf(theta);
1774 const float cos_theta = cosf(theta);
1776 vec[0] = cos_theta * sin_phi;
1777 vec[1] = -sin_theta;
1778 vec[2] = -cos_theta * cos_phi;
1780 normalize_vector(vec);
1784 * Calculate frame position in sinusoidal format for corresponding 3D coordinates on sphere.
1786 * @param s filter private context
1787 * @param vec coordinates on sphere
1788 * @param width frame width
1789 * @param height frame height
1790 * @param us horizontal coordinates for interpolation window
1791 * @param vs vertical coordinates for interpolation window
1792 * @param du horizontal relative coordinate
1793 * @param dv vertical relative coordinate
1795 static void xyz_to_sinusoidal(const V360Context *s,
1796 const float *vec, int width, int height,
1797 uint16_t us[4][4], uint16_t vs[4][4], float *du, float *dv)
1799 const float theta = asinf(-vec[1]) * s->input_mirror_modifier[1];
1800 const float phi = atan2f(vec[0], -vec[2]) * s->input_mirror_modifier[0] * cosf(theta);
1804 uf = (phi / M_PI + 1.f) * width / 2.f;
1805 vf = (theta / M_PI_2 + 1.f) * height / 2.f;
1812 for (int i = -1; i < 3; i++) {
1813 for (int j = -1; j < 3; j++) {
1814 us[i + 1][j + 1] = av_clip(ui + j, 0, width - 1);
1815 vs[i + 1][j + 1] = av_clip(vi + i, 0, height - 1);
1821 * Prepare data for processing equi-angular cubemap input format.
1823 * @param ctx filter context
1825 * @return error code
1827 static int prepare_eac_in(AVFilterContext *ctx)
1829 V360Context *s = ctx->priv;
1831 if (s->ih_flip && s->iv_flip) {
1832 s->in_cubemap_face_order[RIGHT] = BOTTOM_LEFT;
1833 s->in_cubemap_face_order[LEFT] = BOTTOM_RIGHT;
1834 s->in_cubemap_face_order[UP] = TOP_LEFT;
1835 s->in_cubemap_face_order[DOWN] = TOP_RIGHT;
1836 s->in_cubemap_face_order[FRONT] = BOTTOM_MIDDLE;
1837 s->in_cubemap_face_order[BACK] = TOP_MIDDLE;
1838 } else if (s->ih_flip) {
1839 s->in_cubemap_face_order[RIGHT] = TOP_LEFT;
1840 s->in_cubemap_face_order[LEFT] = TOP_RIGHT;
1841 s->in_cubemap_face_order[UP] = BOTTOM_LEFT;
1842 s->in_cubemap_face_order[DOWN] = BOTTOM_RIGHT;
1843 s->in_cubemap_face_order[FRONT] = TOP_MIDDLE;
1844 s->in_cubemap_face_order[BACK] = BOTTOM_MIDDLE;
1845 } else if (s->iv_flip) {
1846 s->in_cubemap_face_order[RIGHT] = BOTTOM_RIGHT;
1847 s->in_cubemap_face_order[LEFT] = BOTTOM_LEFT;
1848 s->in_cubemap_face_order[UP] = TOP_RIGHT;
1849 s->in_cubemap_face_order[DOWN] = TOP_LEFT;
1850 s->in_cubemap_face_order[FRONT] = BOTTOM_MIDDLE;
1851 s->in_cubemap_face_order[BACK] = TOP_MIDDLE;
1853 s->in_cubemap_face_order[RIGHT] = TOP_RIGHT;
1854 s->in_cubemap_face_order[LEFT] = TOP_LEFT;
1855 s->in_cubemap_face_order[UP] = BOTTOM_RIGHT;
1856 s->in_cubemap_face_order[DOWN] = BOTTOM_LEFT;
1857 s->in_cubemap_face_order[FRONT] = TOP_MIDDLE;
1858 s->in_cubemap_face_order[BACK] = BOTTOM_MIDDLE;
1862 s->in_cubemap_face_rotation[TOP_LEFT] = ROT_270;
1863 s->in_cubemap_face_rotation[TOP_MIDDLE] = ROT_90;
1864 s->in_cubemap_face_rotation[TOP_RIGHT] = ROT_270;
1865 s->in_cubemap_face_rotation[BOTTOM_LEFT] = ROT_0;
1866 s->in_cubemap_face_rotation[BOTTOM_MIDDLE] = ROT_0;
1867 s->in_cubemap_face_rotation[BOTTOM_RIGHT] = ROT_0;
1869 s->in_cubemap_face_rotation[TOP_LEFT] = ROT_0;
1870 s->in_cubemap_face_rotation[TOP_MIDDLE] = ROT_0;
1871 s->in_cubemap_face_rotation[TOP_RIGHT] = ROT_0;
1872 s->in_cubemap_face_rotation[BOTTOM_LEFT] = ROT_270;
1873 s->in_cubemap_face_rotation[BOTTOM_MIDDLE] = ROT_90;
1874 s->in_cubemap_face_rotation[BOTTOM_RIGHT] = ROT_270;
1881 * Prepare data for processing equi-angular cubemap output format.
1883 * @param ctx filter context
1885 * @return error code
1887 static int prepare_eac_out(AVFilterContext *ctx)
1889 V360Context *s = ctx->priv;
1891 s->out_cubemap_direction_order[TOP_LEFT] = LEFT;
1892 s->out_cubemap_direction_order[TOP_MIDDLE] = FRONT;
1893 s->out_cubemap_direction_order[TOP_RIGHT] = RIGHT;
1894 s->out_cubemap_direction_order[BOTTOM_LEFT] = DOWN;
1895 s->out_cubemap_direction_order[BOTTOM_MIDDLE] = BACK;
1896 s->out_cubemap_direction_order[BOTTOM_RIGHT] = UP;
1898 s->out_cubemap_face_rotation[TOP_LEFT] = ROT_0;
1899 s->out_cubemap_face_rotation[TOP_MIDDLE] = ROT_0;
1900 s->out_cubemap_face_rotation[TOP_RIGHT] = ROT_0;
1901 s->out_cubemap_face_rotation[BOTTOM_LEFT] = ROT_270;
1902 s->out_cubemap_face_rotation[BOTTOM_MIDDLE] = ROT_90;
1903 s->out_cubemap_face_rotation[BOTTOM_RIGHT] = ROT_270;
1909 * Calculate 3D coordinates on sphere for corresponding frame position in equi-angular cubemap format.
1911 * @param s filter private context
1912 * @param i horizontal position on frame [0, width)
1913 * @param j vertical position on frame [0, height)
1914 * @param width frame width
1915 * @param height frame height
1916 * @param vec coordinates on sphere
1918 static void eac_to_xyz(const V360Context *s,
1919 int i, int j, int width, int height,
1922 const float pixel_pad = 2;
1923 const float u_pad = pixel_pad / width;
1924 const float v_pad = pixel_pad / height;
1926 int u_face, v_face, face;
1928 float l_x, l_y, l_z;
1930 float uf = (i + 0.5f) / width;
1931 float vf = (j + 0.5f) / height;
1933 // EAC has 2-pixel padding on faces except between faces on the same row
1934 // Padding pixels seems not to be stretched with tangent as regular pixels
1935 // Formulas below approximate original padding as close as I could get experimentally
1937 // Horizontal padding
1938 uf = 3.f * (uf - u_pad) / (1.f - 2.f * u_pad);
1942 } else if (uf >= 3.f) {
1946 u_face = floorf(uf);
1947 uf = fmodf(uf, 1.f) - 0.5f;
1951 v_face = floorf(vf * 2.f);
1952 vf = (vf - v_pad - 0.5f * v_face) / (0.5f - 2.f * v_pad) - 0.5f;
1954 if (uf >= -0.5f && uf < 0.5f) {
1955 uf = tanf(M_PI_2 * uf);
1959 if (vf >= -0.5f && vf < 0.5f) {
1960 vf = tanf(M_PI_2 * vf);
1965 face = u_face + 3 * v_face;
2006 normalize_vector(vec);
2010 * Calculate frame position in equi-angular cubemap format for corresponding 3D coordinates on sphere.
2012 * @param s filter private context
2013 * @param vec coordinates on sphere
2014 * @param width frame width
2015 * @param height frame height
2016 * @param us horizontal coordinates for interpolation window
2017 * @param vs vertical coordinates for interpolation window
2018 * @param du horizontal relative coordinate
2019 * @param dv vertical relative coordinate
2021 static void xyz_to_eac(const V360Context *s,
2022 const float *vec, int width, int height,
2023 uint16_t us[4][4], uint16_t vs[4][4], float *du, float *dv)
2025 const float pixel_pad = 2;
2026 const float u_pad = pixel_pad / width;
2027 const float v_pad = pixel_pad / height;
2031 int direction, face;
2034 xyz_to_cube(s, vec, &uf, &vf, &direction);
2036 face = s->in_cubemap_face_order[direction];
2040 uf = M_2_PI * atanf(uf) + 0.5f;
2041 vf = M_2_PI * atanf(vf) + 0.5f;
2043 // These formulas are inversed from eac_to_xyz ones
2044 uf = (uf + u_face) * (1.f - 2.f * u_pad) / 3.f + u_pad;
2045 vf = vf * (0.5f - 2.f * v_pad) + v_pad + 0.5f * v_face;
2059 for (int i = -1; i < 3; i++) {
2060 for (int j = -1; j < 3; j++) {
2061 us[i + 1][j + 1] = av_clip(ui + j, 0, width - 1);
2062 vs[i + 1][j + 1] = av_clip(vi + i, 0, height - 1);
2068 * Prepare data for processing flat output format.
2070 * @param ctx filter context
2072 * @return error code
2074 static int prepare_flat_out(AVFilterContext *ctx)
2076 V360Context *s = ctx->priv;
2078 s->flat_range[0] = tanf(0.5f * s->h_fov * M_PI / 180.f);
2079 s->flat_range[1] = tanf(0.5f * s->v_fov * M_PI / 180.f);
2085 * Calculate 3D coordinates on sphere for corresponding frame position in flat format.
2087 * @param s filter private context
2088 * @param i horizontal position on frame [0, width)
2089 * @param j vertical position on frame [0, height)
2090 * @param width frame width
2091 * @param height frame height
2092 * @param vec coordinates on sphere
2094 static void flat_to_xyz(const V360Context *s,
2095 int i, int j, int width, int height,
2098 const float l_x = s->flat_range[0] * (2.f * i / width - 1.f);
2099 const float l_y = -s->flat_range[1] * (2.f * j / height - 1.f);
2105 normalize_vector(vec);
2109 * Calculate 3D coordinates on sphere for corresponding frame position in dual fisheye format.
2111 * @param s filter private context
2112 * @param i horizontal position on frame [0, width)
2113 * @param j vertical position on frame [0, height)
2114 * @param width frame width
2115 * @param height frame height
2116 * @param vec coordinates on sphere
2118 static void dfisheye_to_xyz(const V360Context *s,
2119 int i, int j, int width, int height,
2122 const float scale = 1.f + s->out_pad;
2124 const float ew = width / 2.f;
2125 const float eh = height;
2127 const int ei = i >= ew ? i - ew : i;
2128 const float m = i >= ew ? -1.f : 1.f;
2130 const float uf = ((2.f * ei) / ew - 1.f) * scale;
2131 const float vf = ((2.f * j) / eh - 1.f) * scale;
2133 const float h = hypotf(uf, vf);
2134 const float lh = h > 0.f ? h : 1.f;
2135 const float theta = m * M_PI_2 * (1.f - h);
2137 const float sin_theta = sinf(theta);
2138 const float cos_theta = cosf(theta);
2140 vec[0] = cos_theta * m * -uf / lh;
2141 vec[1] = cos_theta * -vf / lh;
2144 normalize_vector(vec);
2148 * Calculate frame position in dual fisheye format for corresponding 3D coordinates on sphere.
2150 * @param s filter private context
2151 * @param vec coordinates on sphere
2152 * @param width frame width
2153 * @param height frame height
2154 * @param us horizontal coordinates for interpolation window
2155 * @param vs vertical coordinates for interpolation window
2156 * @param du horizontal relative coordinate
2157 * @param dv vertical relative coordinate
2159 static void xyz_to_dfisheye(const V360Context *s,
2160 const float *vec, int width, int height,
2161 uint16_t us[4][4], uint16_t vs[4][4], float *du, float *dv)
2163 const float scale = 1.f - s->in_pad;
2165 const float ew = width / 2.f;
2166 const float eh = height;
2168 const float h = hypotf(vec[0], vec[1]);
2169 const float lh = h > 0.f ? h : 1.f;
2170 const float theta = acosf(fabsf(vec[2])) / M_PI;
2172 float uf = (theta * (-vec[0] / lh) * s->input_mirror_modifier[0] * scale + 0.5f) * ew;
2173 float vf = (theta * (-vec[1] / lh) * s->input_mirror_modifier[1] * scale + 0.5f) * eh;
2178 if (vec[2] >= 0.f) {
2181 u_shift = ceilf(ew);
2191 for (int i = -1; i < 3; i++) {
2192 for (int j = -1; j < 3; j++) {
2193 us[i + 1][j + 1] = av_clip(u_shift + ui + j, 0, width - 1);
2194 vs[i + 1][j + 1] = av_clip( vi + i, 0, height - 1);
2200 * Calculate 3D coordinates on sphere for corresponding frame position in barrel facebook's format.
2202 * @param s filter private context
2203 * @param i horizontal position on frame [0, width)
2204 * @param j vertical position on frame [0, height)
2205 * @param width frame width
2206 * @param height frame height
2207 * @param vec coordinates on sphere
2209 static void barrel_to_xyz(const V360Context *s,
2210 int i, int j, int width, int height,
2213 const float scale = 0.99f;
2214 float l_x, l_y, l_z;
2216 if (i < 4 * width / 5) {
2217 const float theta_range = M_PI_4;
2219 const int ew = 4 * width / 5;
2220 const int eh = height;
2222 const float phi = ((2.f * i) / ew - 1.f) * M_PI / scale;
2223 const float theta = ((2.f * j) / eh - 1.f) * theta_range / scale;
2225 const float sin_phi = sinf(phi);
2226 const float cos_phi = cosf(phi);
2227 const float sin_theta = sinf(theta);
2228 const float cos_theta = cosf(theta);
2230 l_x = cos_theta * sin_phi;
2232 l_z = -cos_theta * cos_phi;
2234 const int ew = width / 5;
2235 const int eh = height / 2;
2240 uf = 2.f * (i - 4 * ew) / ew - 1.f;
2241 vf = 2.f * (j ) / eh - 1.f;
2250 uf = 2.f * (i - 4 * ew) / ew - 1.f;
2251 vf = 2.f * (j - eh) / eh - 1.f;
2266 normalize_vector(vec);
2270 * Calculate frame position in barrel facebook's format for corresponding 3D coordinates on sphere.
2272 * @param s filter private context
2273 * @param vec coordinates on sphere
2274 * @param width frame width
2275 * @param height frame height
2276 * @param us horizontal coordinates for interpolation window
2277 * @param vs vertical coordinates for interpolation window
2278 * @param du horizontal relative coordinate
2279 * @param dv vertical relative coordinate
2281 static void xyz_to_barrel(const V360Context *s,
2282 const float *vec, int width, int height,
2283 uint16_t us[4][4], uint16_t vs[4][4], float *du, float *dv)
2285 const float scale = 0.99f;
2287 const float phi = atan2f(vec[0], -vec[2]) * s->input_mirror_modifier[0];
2288 const float theta = asinf(-vec[1]) * s->input_mirror_modifier[1];
2289 const float theta_range = M_PI_4;
2292 int u_shift, v_shift;
2296 if (theta > -theta_range && theta < theta_range) {
2300 u_shift = s->ih_flip ? width / 5 : 0;
2303 uf = (phi / M_PI * scale + 1.f) * ew / 2.f;
2304 vf = (theta / theta_range * scale + 1.f) * eh / 2.f;
2309 u_shift = s->ih_flip ? 0 : 4 * ew;
2311 if (theta < 0.f) { // UP
2312 uf = vec[0] / vec[1];
2313 vf = -vec[2] / vec[1];
2316 uf = -vec[0] / vec[1];
2317 vf = -vec[2] / vec[1];
2321 uf *= s->input_mirror_modifier[0] * s->input_mirror_modifier[1];
2322 vf *= s->input_mirror_modifier[1];
2324 uf = 0.5f * ew * (uf * scale + 1.f);
2325 vf = 0.5f * eh * (vf * scale + 1.f);
2334 for (int i = -1; i < 3; i++) {
2335 for (int j = -1; j < 3; j++) {
2336 us[i + 1][j + 1] = u_shift + av_clip(ui + j, 0, ew - 1);
2337 vs[i + 1][j + 1] = v_shift + av_clip(vi + i, 0, eh - 1);
2342 static void multiply_matrix(float c[3][3], const float a[3][3], const float b[3][3])
2344 for (int i = 0; i < 3; i++) {
2345 for (int j = 0; j < 3; j++) {
2348 for (int k = 0; k < 3; k++)
2349 sum += a[i][k] * b[k][j];
2357 * Calculate rotation matrix for yaw/pitch/roll angles.
2359 static inline void calculate_rotation_matrix(float yaw, float pitch, float roll,
2360 float rot_mat[3][3],
2361 const int rotation_order[3])
2363 const float yaw_rad = yaw * M_PI / 180.f;
2364 const float pitch_rad = pitch * M_PI / 180.f;
2365 const float roll_rad = roll * M_PI / 180.f;
2367 const float sin_yaw = sinf(-yaw_rad);
2368 const float cos_yaw = cosf(-yaw_rad);
2369 const float sin_pitch = sinf(pitch_rad);
2370 const float cos_pitch = cosf(pitch_rad);
2371 const float sin_roll = sinf(roll_rad);
2372 const float cos_roll = cosf(roll_rad);
2377 m[0][0][0] = cos_yaw; m[0][0][1] = 0; m[0][0][2] = sin_yaw;
2378 m[0][1][0] = 0; m[0][1][1] = 1; m[0][1][2] = 0;
2379 m[0][2][0] = -sin_yaw; m[0][2][1] = 0; m[0][2][2] = cos_yaw;
2381 m[1][0][0] = 1; m[1][0][1] = 0; m[1][0][2] = 0;
2382 m[1][1][0] = 0; m[1][1][1] = cos_pitch; m[1][1][2] = -sin_pitch;
2383 m[1][2][0] = 0; m[1][2][1] = sin_pitch; m[1][2][2] = cos_pitch;
2385 m[2][0][0] = cos_roll; m[2][0][1] = -sin_roll; m[2][0][2] = 0;
2386 m[2][1][0] = sin_roll; m[2][1][1] = cos_roll; m[2][1][2] = 0;
2387 m[2][2][0] = 0; m[2][2][1] = 0; m[2][2][2] = 1;
2389 multiply_matrix(temp, m[rotation_order[0]], m[rotation_order[1]]);
2390 multiply_matrix(rot_mat, temp, m[rotation_order[2]]);
2394 * Rotate vector with given rotation matrix.
2396 * @param rot_mat rotation matrix
2399 static inline void rotate(const float rot_mat[3][3],
2402 const float x_tmp = vec[0] * rot_mat[0][0] + vec[1] * rot_mat[0][1] + vec[2] * rot_mat[0][2];
2403 const float y_tmp = vec[0] * rot_mat[1][0] + vec[1] * rot_mat[1][1] + vec[2] * rot_mat[1][2];
2404 const float z_tmp = vec[0] * rot_mat[2][0] + vec[1] * rot_mat[2][1] + vec[2] * rot_mat[2][2];
2411 static inline void set_mirror_modifier(int h_flip, int v_flip, int d_flip,
2414 modifier[0] = h_flip ? -1.f : 1.f;
2415 modifier[1] = v_flip ? -1.f : 1.f;
2416 modifier[2] = d_flip ? -1.f : 1.f;
2419 static inline void mirror(const float *modifier, float *vec)
2421 vec[0] *= modifier[0];
2422 vec[1] *= modifier[1];
2423 vec[2] *= modifier[2];
2426 static int allocate_plane(V360Context *s, int sizeof_uv, int sizeof_ker, int p)
2428 s->u[p] = av_calloc(s->uv_linesize[p] * s->pr_height[p], sizeof_uv);
2429 s->v[p] = av_calloc(s->uv_linesize[p] * s->pr_height[p], sizeof_uv);
2430 if (!s->u[p] || !s->v[p])
2431 return AVERROR(ENOMEM);
2433 s->ker[p] = av_calloc(s->uv_linesize[p] * s->pr_height[p], sizeof_ker);
2435 return AVERROR(ENOMEM);
2441 static void fov_from_dfov(V360Context *s, float w, float h)
2443 const float da = tanf(0.5 * FFMIN(s->d_fov, 359.f) * M_PI / 180.f);
2444 const float d = hypotf(w, h);
2446 s->h_fov = atan2f(da * w, d) * 360.f / M_PI;
2447 s->v_fov = atan2f(da * h, d) * 360.f / M_PI;
2455 static void set_dimensions(int *outw, int *outh, int w, int h, const AVPixFmtDescriptor *desc)
2457 outw[1] = outw[2] = FF_CEIL_RSHIFT(w, desc->log2_chroma_w);
2458 outw[0] = outw[3] = w;
2459 outh[1] = outh[2] = FF_CEIL_RSHIFT(h, desc->log2_chroma_h);
2460 outh[0] = outh[3] = h;
2463 // Calculate remap data
2464 static av_always_inline int v360_slice(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
2466 V360Context *s = ctx->priv;
2468 for (int p = 0; p < s->nb_allocated; p++) {
2469 const int width = s->pr_width[p];
2470 const int uv_linesize = s->uv_linesize[p];
2471 const int height = s->pr_height[p];
2472 const int in_width = s->inplanewidth[p];
2473 const int in_height = s->inplaneheight[p];
2474 const int slice_start = (height * jobnr ) / nb_jobs;
2475 const int slice_end = (height * (jobnr + 1)) / nb_jobs;
2480 for (int j = slice_start; j < slice_end; j++) {
2481 for (int i = 0; i < width; i++) {
2482 uint16_t *u = s->u[p] + (j * uv_linesize + i) * s->elements;
2483 uint16_t *v = s->v[p] + (j * uv_linesize + i) * s->elements;
2484 int16_t *ker = s->ker[p] + (j * uv_linesize + i) * s->elements;
2486 if (s->out_transpose)
2487 s->out_transform(s, j, i, height, width, vec);
2489 s->out_transform(s, i, j, width, height, vec);
2490 av_assert1(!isnan(vec[0]) && !isnan(vec[1]) && !isnan(vec[2]));
2491 rotate(s->rot_mat, vec);
2492 av_assert1(!isnan(vec[0]) && !isnan(vec[1]) && !isnan(vec[2]));
2493 normalize_vector(vec);
2494 mirror(s->output_mirror_modifier, vec);
2495 if (s->in_transpose)
2496 s->in_transform(s, vec, in_height, in_width, rmap.v, rmap.u, &du, &dv);
2498 s->in_transform(s, vec, in_width, in_height, rmap.u, rmap.v, &du, &dv);
2499 av_assert1(!isnan(du) && !isnan(dv));
2500 s->calculate_kernel(du, dv, &rmap, u, v, ker);
2508 static int config_output(AVFilterLink *outlink)
2510 AVFilterContext *ctx = outlink->src;
2511 AVFilterLink *inlink = ctx->inputs[0];
2512 V360Context *s = ctx->priv;
2513 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(inlink->format);
2514 const int depth = desc->comp[0].depth;
2519 int in_offset_h, in_offset_w;
2520 int out_offset_h, out_offset_w;
2522 int (*prepare_out)(AVFilterContext *ctx);
2524 s->input_mirror_modifier[0] = s->ih_flip ? -1.f : 1.f;
2525 s->input_mirror_modifier[1] = s->iv_flip ? -1.f : 1.f;
2527 switch (s->interp) {
2529 s->calculate_kernel = nearest_kernel;
2530 s->remap_slice = depth <= 8 ? remap1_8bit_slice : remap1_16bit_slice;
2532 sizeof_uv = sizeof(uint16_t) * s->elements;
2536 s->calculate_kernel = bilinear_kernel;
2537 s->remap_slice = depth <= 8 ? remap2_8bit_slice : remap2_16bit_slice;
2538 s->elements = 2 * 2;
2539 sizeof_uv = sizeof(uint16_t) * s->elements;
2540 sizeof_ker = sizeof(uint16_t) * s->elements;
2543 s->calculate_kernel = bicubic_kernel;
2544 s->remap_slice = depth <= 8 ? remap4_8bit_slice : remap4_16bit_slice;
2545 s->elements = 4 * 4;
2546 sizeof_uv = sizeof(uint16_t) * s->elements;
2547 sizeof_ker = sizeof(uint16_t) * s->elements;
2550 s->calculate_kernel = lanczos_kernel;
2551 s->remap_slice = depth <= 8 ? remap4_8bit_slice : remap4_16bit_slice;
2552 s->elements = 4 * 4;
2553 sizeof_uv = sizeof(uint16_t) * s->elements;
2554 sizeof_ker = sizeof(uint16_t) * s->elements;
2560 ff_v360_init(s, depth);
2562 for (int order = 0; order < NB_RORDERS; order++) {
2563 const char c = s->rorder[order];
2567 av_log(ctx, AV_LOG_ERROR,
2568 "Incomplete rorder option. Direction for all 3 rotation orders should be specified.\n");
2569 return AVERROR(EINVAL);
2572 rorder = get_rorder(c);
2574 av_log(ctx, AV_LOG_ERROR,
2575 "Incorrect rotation order symbol '%c' in rorder option.\n", c);
2576 return AVERROR(EINVAL);
2579 s->rotation_order[order] = rorder;
2582 switch (s->in_stereo) {
2586 in_offset_w = in_offset_h = 0;
2604 set_dimensions(s->inplanewidth, s->inplaneheight, w, h, desc);
2605 set_dimensions(s->in_offset_w, s->in_offset_h, in_offset_w, in_offset_h, desc);
2607 s->in_width = s->inplanewidth[0];
2608 s->in_height = s->inplaneheight[0];
2610 if (s->in_transpose)
2611 FFSWAP(int, s->in_width, s->in_height);
2614 case EQUIRECTANGULAR:
2615 s->in_transform = xyz_to_equirect;
2621 s->in_transform = xyz_to_cube3x2;
2622 err = prepare_cube_in(ctx);
2627 s->in_transform = xyz_to_cube1x6;
2628 err = prepare_cube_in(ctx);
2633 s->in_transform = xyz_to_cube6x1;
2634 err = prepare_cube_in(ctx);
2639 s->in_transform = xyz_to_eac;
2640 err = prepare_eac_in(ctx);
2645 av_log(ctx, AV_LOG_ERROR, "Flat format is not accepted as input.\n");
2646 return AVERROR(EINVAL);
2648 s->in_transform = xyz_to_dfisheye;
2654 s->in_transform = xyz_to_barrel;
2660 s->in_transform = xyz_to_stereographic;
2666 s->in_transform = xyz_to_mercator;
2672 s->in_transform = xyz_to_ball;
2678 s->in_transform = xyz_to_hammer;
2684 s->in_transform = xyz_to_sinusoidal;
2690 av_log(ctx, AV_LOG_ERROR, "Specified input format is not handled.\n");
2699 case EQUIRECTANGULAR:
2700 s->out_transform = equirect_to_xyz;
2706 s->out_transform = cube3x2_to_xyz;
2707 prepare_out = prepare_cube_out;
2708 w = roundf(wf / 4.f * 3.f);
2712 s->out_transform = cube1x6_to_xyz;
2713 prepare_out = prepare_cube_out;
2714 w = roundf(wf / 4.f);
2715 h = roundf(hf * 3.f);
2718 s->out_transform = cube6x1_to_xyz;
2719 prepare_out = prepare_cube_out;
2720 w = roundf(wf / 2.f * 3.f);
2721 h = roundf(hf / 2.f);
2724 s->out_transform = eac_to_xyz;
2725 prepare_out = prepare_eac_out;
2727 h = roundf(hf / 8.f * 9.f);
2730 s->out_transform = flat_to_xyz;
2731 prepare_out = prepare_flat_out;
2736 s->out_transform = dfisheye_to_xyz;
2742 s->out_transform = barrel_to_xyz;
2744 w = roundf(wf / 4.f * 5.f);
2748 s->out_transform = stereographic_to_xyz;
2749 prepare_out = prepare_stereographic_out;
2751 h = roundf(hf * 2.f);
2754 s->out_transform = mercator_to_xyz;
2757 h = roundf(hf * 2.f);
2760 s->out_transform = ball_to_xyz;
2763 h = roundf(hf * 2.f);
2766 s->out_transform = hammer_to_xyz;
2772 s->out_transform = sinusoidal_to_xyz;
2778 av_log(ctx, AV_LOG_ERROR, "Specified output format is not handled.\n");
2782 // Override resolution with user values if specified
2783 if (s->width > 0 && s->height > 0) {
2786 } else if (s->width > 0 || s->height > 0) {
2787 av_log(ctx, AV_LOG_ERROR, "Both width and height values should be specified.\n");
2788 return AVERROR(EINVAL);
2790 if (s->out_transpose)
2793 if (s->in_transpose)
2798 fov_from_dfov(s, w, h);
2801 err = prepare_out(ctx);
2806 set_dimensions(s->pr_width, s->pr_height, w, h, desc);
2808 s->out_width = s->pr_width[0];
2809 s->out_height = s->pr_height[0];
2811 if (s->out_transpose)
2812 FFSWAP(int, s->out_width, s->out_height);
2814 switch (s->out_stereo) {
2816 out_offset_w = out_offset_h = 0;
2832 set_dimensions(s->out_offset_w, s->out_offset_h, out_offset_w, out_offset_h, desc);
2833 set_dimensions(s->planewidth, s->planeheight, w, h, desc);
2835 for (int i = 0; i < 4; i++)
2836 s->uv_linesize[i] = FFALIGN(s->pr_width[i], 8);
2841 s->nb_planes = av_pix_fmt_count_planes(inlink->format);
2843 if (desc->log2_chroma_h == desc->log2_chroma_w && desc->log2_chroma_h == 0) {
2844 s->nb_allocated = 1;
2845 s->map[0] = s->map[1] = s->map[2] = s->map[3] = 0;
2847 s->nb_allocated = 2;
2849 s->map[1] = s->map[2] = 1;
2853 for (int i = 0; i < s->nb_allocated; i++)
2854 allocate_plane(s, sizeof_uv, sizeof_ker, i);
2856 calculate_rotation_matrix(s->yaw, s->pitch, s->roll, s->rot_mat, s->rotation_order);
2857 set_mirror_modifier(s->h_flip, s->v_flip, s->d_flip, s->output_mirror_modifier);
2859 ctx->internal->execute(ctx, v360_slice, NULL, NULL, FFMIN(outlink->h, ff_filter_get_nb_threads(ctx)));
2864 static int filter_frame(AVFilterLink *inlink, AVFrame *in)
2866 AVFilterContext *ctx = inlink->dst;
2867 AVFilterLink *outlink = ctx->outputs[0];
2868 V360Context *s = ctx->priv;
2872 out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
2875 return AVERROR(ENOMEM);
2877 av_frame_copy_props(out, in);
2882 ctx->internal->execute(ctx, s->remap_slice, &td, NULL, FFMIN(outlink->h, ff_filter_get_nb_threads(ctx)));
2885 return ff_filter_frame(outlink, out);
2888 static av_cold void uninit(AVFilterContext *ctx)
2890 V360Context *s = ctx->priv;
2892 for (int p = 0; p < s->nb_allocated; p++) {
2895 av_freep(&s->ker[p]);
2899 static const AVFilterPad inputs[] = {
2902 .type = AVMEDIA_TYPE_VIDEO,
2903 .filter_frame = filter_frame,
2908 static const AVFilterPad outputs[] = {
2911 .type = AVMEDIA_TYPE_VIDEO,
2912 .config_props = config_output,
2917 AVFilter ff_vf_v360 = {
2919 .description = NULL_IF_CONFIG_SMALL("Convert 360 projection of video."),
2920 .priv_size = sizeof(V360Context),
2922 .query_formats = query_formats,
2925 .priv_class = &v360_class,
2926 .flags = AVFILTER_FLAG_SLICE_THREADS,